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>
โ๏ธ Step 2: Allow Overrides in Apache Config
Open your virtual host file:
/etc/apache2/sites-available/000-default.conf
Inside the directory block, enable:
AllowOverride All
Then activate mod_rewrite:
sudo a2enmod rewrite
sudo systemctl restart apache2
๐ 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
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;
}
}
โ๏ธ Step 2: Apply & Reload
sudo nginx -t
sudo systemctl reload nginx
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:
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:
Happy coding & smooth deployments! โก๐ฅ
Top comments (0)