How to remove /public from Laravel URL? [SOLVED]

Steve Alila


Different methods to remove /public from Laravel URL

Usually, Laravel URLs tend to be a bit boring. They have a "/public" section that makes them long and visually uninteresting. You can erase /public from Laravel URLs though. And this will give you cleaner, much more user-friendly URLs.
There are quite a few ways to remove the /public segment from a Laravel application's URL:

  1. Apache Virtual Host Method: Modify the Apache’s Virtual Host and directly link it with Laravel's public directory. This is both safe and common for you to use.
  2. .htaccess Method: Transfer every file from the public directory of Laravel to the root. Then alter the .htaccess file so that it routes through index.php. It saves time while developing, but DO NOT use this method when in production as it is very unsafe. However, you can add rewrite rules to automatically forward requests to the public directory as an alternative which is quicker.
  3. Nginx Configuration Method: Edit Nginx's configuration file and set the web root to Laravel's public directory if you’re using high-performance and secure solutions suitable for production environments.
  4. Symbolic Link Method: Create a symbolic link that points to the public directory, useful in shared hosting environments where server configuration control is limited.
  5. Middleware Method: Implement a custom middleware on your Laravel app and rewrite URLs programmatically for an unconventional way of doing things.
  6. Cloud Hosting & Containers: Set the web root directly through configuration files or settings when deploying on cloud platforms or using containers like Docker. This aligns with cloud-native best practices.

In this blog post, we will explore the step-by-step process of removing "/public/" from a Laravel URL and discuss the benefits it brings to your application. Let's dive right in!


Method 1: Using Apache's Virtual Host

Alrighty, let’s get into the nitty-gritty of Apache Virtual Host. It’s a handy feature from Apache that lets you host multiple websites on one server. And that is downright essential when you’re trying to take the public out of your URL in a Laravel project. Do it right and you can point your domain straight to the Laravel project’s public directory, removing the /public segment from your URL altogether

Here, we will look at the detailed steps to effectively remove public from URL in a Laravel application using Apache's Virtual Host.

1. Editing the Apache Configuration

Launch a terminal and start typing this command to open Apache’s configuration file. On Linux systems, you’ll typically find the file at /etc/apache2/sites-available/000-default.conf.

sudo nano /etc/apache2/sites-available/your_project.conf

If you are creating a new configuration file, make sure to enable it later.

2. Updating the Document Root

Find the DocumentRoot directive and update it to point to the public directory of your Laravel project. The updated Virtual Host file should look something like this:

<VirtualHost *:80>
    ServerName your_domain_or_ip
    DocumentRoot /path/to/laravel-project/public

    <Directory /path/to/laravel-project/public>
        AllowOverride All
        Require all granted

3. Enabling Site and Rewrite Module

Once you are done editing the configuration file, go ahead and enable your site and Apache rewrite module – if it isn’t already enabled. The rewrite module is a crucial element in this process to remove public from URL in Laravel.

sudo a2ensite your_project.conf
sudo a2enmod rewrite

4. Restarting Apache

For the changes to take effect, you'll need to restart Apache.

sudo systemctl restart apache2

And voila! You've successfully managed to remove public from URL in your Laravel application using Apache's Virtual Host.


Method 2: Using .htaccess

The .htaccess file is a configuration file that is used by Apache web servers to control the directory it’s in and its subdirectories. It’s perfect for URL shortening, redirection and other uses. In Laravel, the .htaccess file comes in handy when you’re trying to remove public from your URL but cannot edit Nginx or Apache server configurations.

Below are the steps to remove public from URL using the .htaccess file method:

Moving Files

Backup your Laravel project: Before you proceed, make sure to back up your entire Laravel project to avoid accidental data loss.

Move Files: Move all the files and folders from the public directory to the root directory of your Laravel application. This includes index.php, .htaccess, and other assets like CSS, JS, and images.

Editing .htaccess

Create/Edit .htaccess in Root: If there isn't an .htaccess file in the root directory of your Laravel project, create one. If there is, edit it.

Method-1: Move all files from /public to / (root) directory

Update .htaccess File: Add the following lines to your .htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

Explanation of the Rules

  • <IfModule mod_rewrite.c>: Checks if the Apache module mod_rewrite is enabled. The directives inside this block are only processed if this module is active.
  • <IfModule mod_negotiation.c>: Checks if the Apache module mod_negotiation is enabled. The directives inside this block are only processed if this module is active.
  • Options -MultiViews -Indexes: Disables content negotiation and directory listing. This is a security measure to prevent unwanted listing of files and directories.
  • RewriteEngine On: Enables the rewrite engine so that URL rewriting can be performed based on the following rules.
  • RewriteRule ^(.*)/$ /$1 [L,R=301]: This rule removes trailing slashes from URLs, which can help with SEO. The [L,R=301] flags indicate that this is the "last" rule that should be processed and a permanent (301) redirection should be issued.
  • RewriteCond %{REQUEST_FILENAME} !-d: Checks that the request is not for a directory. The !-d means "not a directory".
  • RewriteCond %{REQUEST_FILENAME} !-f: Checks that the request is not for a file. The !-f means "not a file".
  • RewriteRule ^ index.php [L]: If conditions 6 and 7 are met (the request is neither for a directory nor for a file), this rule rewrites the request to index.php. The [L] flag indicates that this is the last rule that should be processed if the conditions are met.

By following these rules, you’ll make sure that all web traffic goes through Laravel’s front controller (index.php) and use Apache for routing. This will allow you to get rid of /public in the URL.


  • Easy to implement if you don’t have access to server configuration.
  • A quick fix for development environments.


  • Not recommended for production because it might pose a security risk.
  • Permission issues may arise.
  • Not an industry-standard way of handling URL restructuring.

Method-2: Forward requests to /public URL

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]

The code snippet checks if mod_rewrite is enabled, and if so, it enables the rewriting engine. It then checks if the requested URL does not already contain "/public/". If the condition is met, the rule rewrites the URL to include the "/public/" segment. This allows the Laravel application to be accessed without explicitly typing "/public/" in the URL while preserving the original requested path.

Here is what each line does:

  • <IfModule mod_rewrite.c>: This directive checks if the mod_rewrite module is installed and enabled in the Apache server. It ensures that the subsequent code is only executed if the module is available.
  • RewriteEngine On: This directive enables the URL rewriting engine, allowing for the manipulation of URLs based on specified rules.
  • RewriteCond %{REQUEST_URI} !^/public/: This line specifies a rewrite condition using the %{REQUEST_URI} server variable. It checks if the requested URL does not already start with "/public/". This condition prevents an infinite loop by excluding URLs that already have the "/public/" segment.
  • RewriteRule ^(.*)$ /public/$1 [L,QSA]: This line defines the actual rewrite rule. It captures the entire requested URL pattern (using the regular expression ^(.*)$) and rewrites it to prepend "/public/" to the URL. The [L,QSA] flags are used to specify that this is the last rule to be processed and to append any query strings (QSA stands for "query string append").
  • </IfModule>: This closing directive marks the end of the conditional block and ensures that the code inside it is only executed if the mod_rewrite module is available.

Why This Method is Not Recommended for Production

While this method to remove public from URL might seem convenient, it's generally not recommended for production environments for several reasons:

  • Security Risks: By moving files to the root directory, you're potentially exposing sensitive configuration files and code to the public.
  • Maintainability: Future Laravel or package updates may require additional or different files in the public directory, making this approach hard to maintain.
  • Performance: Extra .htaccess file checks and rewrites could lead to slower server response times.


Method 3: Using Nginx

Nginx, or as some would say “Engine-X”, fits the bill as a high-performance web server. It is also a reverse proxy and load balancer. Due to it’s lightweight structure and ability to handle huge amounts of traffic, it is growing more popular by the day. When dealing with Laravel, Nginx can be configured to remove public from URL. This could allow HTTP requests to go straight to a public directory and bring your app’s URL closer within reach.

Here is a comprehensive guide on how to remove public from URL when using Nginx as your web server:

1. Editing Nginx Configuration

Open Nginx Configuration: Use the terminal to open your Nginx configuration file. This could be located at /etc/nginx/sites-available/default, /etc/nginx/nginx.conf, or a custom file you've created under sites-available.

sudo nano /etc/nginx/sites-available/your_project

2. Updating Configuration

Modify Server Block: In the configuration file, look for the server block and update it as follows:

server {
    listen 80;
    server_name your_domain_or_ip;

    root /path/to/laravel-project/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

Here, the root directive is updated to point directly to the Laravel project's public directory, allowing us to remove public from URL.

3. Testing and Restarting Nginx

Test Configuration: Always test your configuration file for syntax errors before restarting the service.

sudo nginx -t

Restart Nginx: If the test is successful, restart Nginx to apply the changes.

sudo systemctl restart nginx


  • High performance and resource-efficient, especially for high-traffic sites.
  • More control over web server behavior.
  • Considered best practice for production environments.


  • Slightly more complex configuration compared to Apache.
  • Might require more initial setup like installing PHP-FPM separately.


Laravel Application Configuration Impacts

Though we primarily focused on server configuration, but these changes generally won't require any adjustments to the Laravel app itself. However, here are few Laravel-specific points you might want to consider while removing /public from the URL.

  • Environment Configuration: If your .env file has hardcoded URLs, then you’ll likely have to update them after removing /public from the URL.
  • Asset & URL Helpers: If you use Laravel helpers like url(), asset(), or route() for generating URLs in Laravel, then these should work correctly after changing server configuration - these helpers generate URLs based on the application’s base URL that is already configured.
  • Web Server Configuration: Both Apache and Nginx will require you to set the public directory in their configuration files. There shouldn’t be any Laravel specific changes needed.
  • Testing: Make sure all your routes, assets and AJAX requests work as expected after applying any method. It would be worth spending time testing it thoroughly to avoid any surprises later on.
  • Cache & Optimization: Clearing and caching configurations with php artisan config:cache and php artisan route:cache respectively is a good idea after applying this change (especially if you notice anything out of place).
  • Deployment Scripts: You may need to update your automated deployment scripts if they assume certain paths.

If you have hard-coded URLs or paths within your application logic or configuration, you would need to update those accordingly. In some non-standard setups, you might see the 'url' => env('APP_URL', 'http://localhost') configuration in app.php. If you had previously included /public in this URL, then you'd need to update it.

To be on the safe side, always clear your configuration cache after making changes to any .env or configuration files using the following command:

php artisan config:clear


Common Pitfalls & Troubleshooting

Permission Issues

  1. Ownership: Ensure the web server user (often www-data for Apache and Nginx) has the appropriate permissions to read and execute files in the Laravel directory.
  2. File Permissions: Incorrect file permissions could lead to 403 Forbidden errors. Usually, directories should be 755 and files should be 644.

Configuration Mistakes

  • Incorrect Document Root: Ensure the web server's document root is correctly pointed to the public directory in case of Virtual Host or Nginx configurations.
  • Syntax Errors in .htaccess or Nginx Config: A small typo could cause the server to not recognize your new configuration, leading to errors. Check logs for clues.
  • Cache: Web server or Laravel configuration cache could make it seem like changes aren't taking effect. Make sure to clear cache after making changes.

Debugging Steps

  • Check Logs: Both web server logs and Laravel logs (storage/logs) can offer hints on what might be wrong.
  • Config Test: For Apache, use apachectl configtest to test the configuration syntax. For Nginx, use nginx -t.
  • Web Server Restart: Don't forget to restart your web server after making changes to its configuration files.
  • Use Developer Tools: Network tabs in browsers can show you the exact request and response, which can help debug issues.

Frequently Encountered Errors

  • 403 Forbidden: Usually indicates a permissions issue or incorrect directory settings in web server configurations.
  • 404 Not Found: Indicates that the requested resource could not be found on the server, possibly due to incorrect rewrite rules or document root.
  • 500 Internal Server Error: Often caused by syntax errors in .htaccess or Nginx configuration files, or incorrect file permissions.
  • 502 Bad Gateway: For Nginx users, this usually indicates that Nginx couldn't communicate with your PHP-FPM server, possibly due to incorrect settings in your Nginx configuration.



Deleting the /public section from your Laravel software's URL is about more than just how it looks, it’s also a way to make user interactions simpler and enhance security. You’ve got plenty of options to do this, with pros and cons for each. Whether you’re working in production or development, you need to pick the one that’s right for your needs.

  • Apache Virtual Host Method: A recommended option for production, and generally better from a security standpoint.
  • .htaccess Methods: These are good if you want something that works fast and can be used in production. Just know that there are some security issues to consider.
  • Nginx Configuration: If you like high performance systems that work well in production but have extra attention paid to security, this is what you’re looking for.
  • Troubleshooting: It doesn’t matter which option you go with — expect things to get scrapped up along the way. Make sure you have a plan in place for dealing with common mistakes like permission problems and misconfigurations.


Additional Resources

  1. Laravel Official Documentation: The most reliable source for best practices to remove public from URL.
  2. Apache and .htaccess: Detailed guides on how to remove public from URL using Apache configurations.
  3. Nginx Configuration: Official Nginx documentation provides robust methods to remove public from URL.


Views: 265

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages