Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

removing public from the url

Hello,

I tried to remove public from the url and found this link:

https://qastack.id/programming/23837933/how-can-i-remove-public-index-php-in-the-url-generated-laravel

Only that it is in Indonesian. I try to translate Option 2 to English perhaps if it makes any sense? (I understand the rest of it besides this one):

		Option 2:  Move something to the directory '/public' to the root directory

		Create a folder in the root directory and move ll the files and folder expect for the public folder.  You can 	call it anything you want.  In this case, I use "laravel code".

		Next, move everything from the public directory and to the root folder.  It will result something like this:
0 likes
12 replies
webrobert's avatar

Pagi @davy_yg,

When you deploy your site you are meant to point the domain to the public folder. Not the root of the project.

1 like
Tray2's avatar

The best way to to it is to change the document root on your web server so that it points to the public folder

In apache it probably looks something like this

DocumentRoot "/var/www/html"

Change it to

DocumentRoot "/var/www/html/public"

In nginx it probably looks something like this

root /var/www

Change it to

root /var/www/public

Just google, "how to set document root in " (change to apache or nginx).

If you can't change it in Apache you can use the htaccess file to reroute any calls to your document root to public but it's not recommended.

#Rewrite everything to subfolder 
RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/public 
Rewriterule ^(.*)$ public/ [L]

If you aren't allowed to change the document root on your host, I suggest you change host.

1 like
davy_yg's avatar
Level 27

@Tray2 Where to find the DocumentRoot "/var/www/html" in XAMPP?

I have searched in php.ini and my.ini and cannot find it

Tray2's avatar

@davy_yg Dude, do you even know how to use Google?

The document root is defined in the httpd.conf file probalby located here if you followed the recommendations. c:\XAMPP\apache\conf\httpd.conf

2 likes
davy_yg's avatar
Level 27

@Tray2 The only DocumentRoot I can find in c:\XAMPP\apache\conf\httpd.conf:

DocumentRoot "E:/xampp75/htdocs"

Tray2's avatar

@davy_yg exactly and change that to

DocumentRoot "E:/xampp75/htdocs/public"

1 like
davy_yg's avatar
Level 27

I am using XAMPP

Will the example in the tutorial works?

jlrdw's avatar

@Snapey This very question was answered for @davy_yg here:

https://laracasts.com/discuss/channels/laravel/removing-public-folder-in-laravel-local?page=1&replyId=734125

You even got best answer.

Edit

@jeffreyway could you do a short video showing how to point to public as document root in Apache and Nginx, as this question comes up so often. I have answered it countless times and so has others here. Especially Apache since Taylor only has an Nginx example in the documentation.

2 likes
frankielee's avatar

Just wondering, did you notice that the path ""E:/xampp75/htdocs/dashboard/docs" containing the html and pdf files name configure-vhosts.html or configure-vhosts.pdf

You can just open the pdf file just via the url http://localhost/dashboard/docs/configure-vhosts.html

By following the guide, I am able to serve laravel projects correctly 😅.

jlrdw's avatar

@davy_yg what point to public as document root means is:

Look at this nginx example:

server {
            listen 81;
            server_name laravel.local;
            # 192.168.0.1

            

            root /nginx/www/laravel842/public;  <----- HERE you point to public

            location / {
                index index.html index.htm index.php;
                try_files $uri $uri/ /index.php?$query_string;
                }
		
                // more lines of code

However public isn't in URL. Do similar following Apache documentation.

1 like
davy_yg's avatar
davy_yg
OP
Best Answer
Level 27

I finally found the answer by using .htaccess, someone advice to do so:

.htaccess

	<IfModule mod_rewrite.c>
		#Session timeout

	<IfModule mod_negotiation.c>
		Options -MultiViews
	</IfModule>

	RewriteEngine On

	RewriteCond %{REQUEST_FILENAME} -d [OR]
	RewriteCond %{REQUEST_FILENAME} -f
	RewriteRule ^ ^ [N]

	RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
	RewriteRule ^(.*)$ public/

	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule ^ server.php

	</IfModule>

Please or to participate in this conversation.