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)