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)