How to Remove the Public Path from URL in Laravel Application?
When hosting a Laravel application, a common issue developers face is the public
directory appearing in the URL. By default, Laravel serves content from the public
folder, but it’s often desirable to access your application directly from the root directory. This blog post will guide you through the steps to achieve this, including a solution using the .htaccess
file, which can be particularly helpful when hosting on cPanel.
Prerequisites:
- A Laravel application set up on your server.
- Access to your server’s configuration files (e.g.,
httpd.conf
for Apache ornginx.conf
for Nginx). - Basic knowledge of server configuration.
Solution 1: Move Public Files to Root Directory
Step 1: Move Public Files to Root Directory
Navigate to Your Laravel Project.
cd /path/to/your/laravel/project
Move all contents from the public
folder to the root of your Laravel project.
mv public/* .
mv public/.* .
Step 2 : Update index.php
Open the index.php
file that you moved to the root directory and update the paths to the vendor
and bootstrap
directories.
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Step 3 : Update Server Configuration
For Apache :
Open your Apache configuration file (httpd.conf
, or the specific virtual host configuration file) and set the DocumentRoot
to the Laravel root directory.
DocumentRoot /path/to/your/laravel/root
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Restart the Apache server to apply the changes.
sudo systemctl restart apache2
For Nginx :
Open your Nginx configuration file (usually found in /etc/nginx/sites-available/
or /etc/nginx/conf.d/
) and set the root
directive to the Laravel root directory.
server {
listen 80;
server_name your_domain.com;
root /path/to/your/laravel/root;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Restart the Nginx server to apply the changes.
sudo systemctl restart nginx
Solution 2: Use .htaccess to Route Requests
If you are hosting on cPanel, or prefer to keep the public directory structure intact, you can use an .htaccess
file to rewrite URLs and remove the public
path.
Step 1 : Copy and Modify .htaccess
- Copy .htaccess from Public Folder:
Copy the.htaccess
file from thepublic
folder to the root of your Laravel project. - Edit the .htaccess File:
Open the.htaccess
file in the root directory and add the following lines:
Restart the Apache server to apply the changes.
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Remove public URL from the path
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
These two lines are crucial for redirecting all requests to the public
folder:
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
Conclusion :
By following these steps, you can configure your Laravel application to be accessed directly from the root directory, avoiding the public
folder. Whether you prefer moving files or using .htaccess
, both solutions help simplify your URLs and improve your application’s structure.
Happy coding!