DEV Community

Neeraj Saini
Neeraj Saini

Posted on

๐Ÿš€ Clean Laravel URLs & Force HTTPS (Apache + Nginx Guide) โ€” By HaxNeeraj

When you deploy a fresh Laravel project, it serves your application through the /public directory. This leads to URLs like:

https://yourdomain.com/public/login

Such URLs donโ€™t look professional, can negatively impact SEO, and may reduce user confidence.

In this revised guide, youโ€™ll learn how to hide the public directory and enforce HTTPS redirection on both Apache and Nginx servers.
To simplify everything, Iโ€™ve also prepared a full configuration repository you can directly use:

๐Ÿ‘‰ GitHub Repository:
https://github.com/haxneeraj/how-to-remove-public-from-laravel-urls-and-redirect-to-https-using-htaccess


โญ Advantages of Removing โ€œpublicโ€ From Laravel URLs

  • More polished and professional-looking URLs
  • Better SEO visibility
  • Enhanced trust and credibility
  • Prevents exposing internal directory structure
  • Helps avoid asset and routing issues

๐Ÿ” Why Enforce HTTPS Redirection?

Running a production-grade application without HTTPS is not recommended. Hereโ€™s why HTTPS is important:

  • Protects user data using encryption
  • Improves Google search ranking
  • Required by many modern APIs
  • Ensures browser compatibility
  • Builds trust and reliability

๐Ÿ—๏ธ Apache Setup: Remove /public & Enable HTTPS

โœ”๏ธ Step 1: Add .htaccess in the Laravel Root Directory

Paste the following rules in the .htaccess file located in Laravelโ€™s main folder:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect all HTTP requests to HTTPS
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Route requests to public folder without showing it in the URL
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

โœ”๏ธ Step 2: Allow Overrides in Apache Config

Open your virtual host file:

/etc/apache2/sites-available/000-default.conf
Enter fullscreen mode Exit fullscreen mode

Inside the directory block, enable:

AllowOverride All
Enter fullscreen mode Exit fullscreen mode

Then activate mod_rewrite:

sudo a2enmod rewrite
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Nginx Setup: Redirect to HTTPS & Hide /public

Nginx doesnโ€™t use .htaccess, so everything is configured inside the server block.

โœ”๏ธ Step 1: Set Document Root to Laravelโ€™s Public Folder

Edit your Nginx site config:

sudo nano /etc/nginx/sites-available/your-site.conf
Enter fullscreen mode Exit fullscreen mode

Use the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    # Redirect all HTTP traffic to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    root /var/www/your-project/public;
    index index.php index.html;

    # Laravel routes
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP processing
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    }

    # Prevent access to hidden files
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
Enter fullscreen mode Exit fullscreen mode

โœ”๏ธ Step 2: Apply & Reload

sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Your Laravel app will now run without /public and will automatically switch to HTTPS.


๐ŸŽฏ Apache vs Nginx โ€” Quick Comparison

Feature Apache Nginx
Supports .htaccess โœ”๏ธ โŒ
Per-directory config โœ”๏ธ โŒ
High performance Good Excellent
Lightweight โŒ โœ”๏ธ
Laravel compatible โœ”๏ธ โœ”๏ธ

๐Ÿ“ˆ SEO & Performance Improvements

After applying these configurations:

  • URLs become cleaner and more indexable
  • HTTPS helps boost rankings
  • Faster routing and asset delivery
  • Better overall user experience
  • A more secure and optimized environment

Track improvements using:

  • Google Search Console
  • PageSpeed Insights
  • Ahrefs / Semrush

Target keywords such as:

  • Laravel remove public directory
  • Laravel Nginx HTTPS redirect
  • Laravel deployment best practices

๐Ÿ“ Download the Ready-Made Files

Grab all configuration files directly from my GitHub repository:

๐Ÿ‘‰ https://github.com/haxneeraj/how-to-remove-public-from-laravel-urls-and-redirect-to-https-using-htaccess

Donโ€™t forget to โญ star the repo if it saves you time โ€” it motivates me to create more developer tools and guides.


๐Ÿš€ Final Words

Hiding Laravelโ€™s /public directory and enforcing HTTPS are essential steps when deploying your application on any production server. These small tweaks greatly improve SEO, security, and overall professionalism.

Use this guide for both Apache and Nginx, or simply download the ready-made configs from the GitHub repo:

๐Ÿ‘‰ https://github.com/haxneeraj/how-to-remove-public-from-laravel-urls-and-redirect-to-https-using-htaccess

Happy coding & smooth deployments! โšก๐Ÿ”ฅ

Top comments (0)