Search code examples
laravel.htaccessvesta

Laravel .htaccess redirect all in one (1.public 2.https 3.www→non-www)


  1. I have laravel application where .htaccess is redirecting to Laravel's /public folder.
  2. Today I've added the SSL support, and I successfully updated .htaccess to also redirect http to https.
  3. In addition to that I need it to re-write the www to non-www. There are plenty of examples how to do it individually. But I cannot find how to do it in conjunction with the first 2 requirements. Please help.
    my current htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# for HTTPS
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#/ end of HTTPS

RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Solution

  • I updated your existing .htaccess

    RewriteCond %{HTTPS} !^on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
    
    RewriteRule ^(.*)$ public/$1 [L]