Press ESC to close

Manoj Bist

Laravel – Remove Public From Url

 

Laravel Remove Public From URL

All version of Laravel comes with Public directory in it holding an index.php file, which is a very first entry point of Laravel application. From where Laravel loads autoload files to process your request.
Also, Laravel’ Public directory is a place to store your public files like your theme’ stylesheets, javascript files, and images. Including all your publicly accessible files.

There are many tutorials to guide your How to Remove Public From Laravel Url, which says move the index.php file from the Public directory and paste it under your root directory and move htaccess file public directory to root directory. You can do that too. it’s simple and easy unless you want to follow the standard procedure for removing the public from Laravel URL.

To remove Laravel’s public from the URL, you can create a virtual host. that means pointing your server to your laravel’s root directory.

This is how you can remove the public from the laravel URL.

Go to your xamp installation directory and find the following url

xampp/apache/conf/extra/httpd-vhosts.conf


open the above file and add the following code

<VirtualHost *:80>
    ServerAdmin mail@food.test
    DocumentRoot “D:/xamp7/htdocs/food/public”
    ServerName food.test
    ServerAlias *.food.test
    <Directory “D:/xamp7/htdocs/food/public”>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Here htdocs/food  is my Laravel installation directory and with the above code snippet I just pointed my server’s root directory to laravel’s public directory and now whenever i enter the URL, http://food.test, my server will point my request to Laravel’s public directory.

But above code snippet is unable to serve your website unless you tell your computer that the food.test is not online its inside your local server so no need to find it on the internet, just try to find it in my xamp’s htdoc directory. 

Here how you can tell your computer to find domain inside your local server.

Find this file  

c::/windows/system32/drivers/etc/hosts


And add this line at last

127.0.0.1    food.test

It is done now. Now you just need to enter http://food.test.


What if you are on cPanel (Dedicated Server) 

You can ask your hosting provider to modify your cPanel’s root directory. so that it becomes

yourusername/home/public_html/public 

What if you are on Shared Server

Cut out all files from the Public to the root directory.

1. Renaming the server.php to index.php (no modifications)
2. Copy the .htaccess from the public folder to the root folder 
3. Changing .htaccess  a bit as follows for statics:
————————————————————————————- 
    RewriteEngine On
    
# Redirect Trailing Slashes If Not A Folder…
    
RewriteCond %{REQUEST_FILENAME} !-d
    
RewriteRule ^(.*)/$ /$1 [L,R=301]
    
# Handle Front Controller…
    
RewriteCond %{REQUEST_URI} !(.css|.js|.png|.jpg|.gif|robots.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
    
RewriteCond %{REQUEST_FILENAME} !-f
    
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
    
RewriteCond %{REQUEST_FILENAME} !-f
    
RewriteCond %{REQUEST_URI} !^/public/
    
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
    
# Handle Authorization Header
    
RewriteCond %{HTTP:Authorization} .
    
RewriteRule .* – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

If you are a lazy person like me, you can some batch script on GitHub for doing this for you.






Manoj Bist

A Passionate Web Developer/Designer With over 6 years of experience in the industry, Manoj Bist is a seasoned professional who combines technical expertise with a down-to-earth demeanor. As a lover of both technology and design, he thrives on creating seamless digital experiences that captivate and inspire.

Leave a Reply

Your email address will not be published. Required fields are marked *