Best Method to Remove Public From URL in your Laravel Application

Laravel

We have seen in many cases that a developer changes the Laravel system files like server.php to index.php and changes the path in files and so on. We strongly urge you not to do that. That practice will only put the security of your Application at risk.
Here at Fixwebnode, as part of our Website Development Services, we regularly help our Customers to perform related Laravel Application queries.
In this context, we shall look into how to configure Laravel public url extension from your website.

Different ways to Remove Public From URL Laravel

You can remove public from URL in laravel by apply any of the below methods:

  • Create .htaccess file and update content.
  • Raname server.php and move .htaccess file.
  • Change DocumentRoot in the web server configuration file.

1. How to Create .htaccess file and update content ?
You can find .htaccess file in root directory.
You must have mod_rewrite enable on your Apache server. The rewrite module is required to apply these settings. You also have enabled .htaccess in Apache virtual host for Laravel.
Now, Update the code into your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]


2. How to Rename server.php and move .htaccess file ?
Here, you can follow the below steps to remove public from url in laravel:
Start by Renaming server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root folder.

3. Modify DocumentRoot in the web server configuration file. If using Nginx, you will see that the configuration file looks like this:
server {

# Port that the web server will listen on.
listen 80;

# Host that will serve this project.
server_name app.dev;

# Useful logs for debug.
access_log /path/to/access.log;
error_log /path/to/error.log;
rewrite_log on;

# The location of our projects public directory.
root /path/to/our/public;

# Point index to the Laravel front controller.
index index.php;

location / {

# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;

}

# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}

# PHP FPM configuration.
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}

# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;

}
}
So, in the "The location of our projects public directory" section, place the url will public extension.
This will remove it from the url. This is the best method to implement it.


[Need help in fixing Laravel configuration issues? We can help you. ]


Conclusion

This article covers the different methods to remove the public from URL in laravel. In fact, the best way is to place Laravel's files outside your web server root and make the Laravel's public directory your web server root.

Your Cart