Laravel Redirect Http to Https using .htaccess file

Laravel Redirect Http to Https using .htaccess file; Now a days all of websites running on https, but by default our domain running as http so we need to redirect http to https using htaccess file.

To redicting in https we need to SSL certificate, which will secure your application. When you apply ssl certificate in your domain and hit the urls in brower with https, then the website will show Secure. But we need to redirect it from htaccess file so the web url automatically redirect to https.

Laravel provide us the .htaccess file which will redirect the http protocol to https in laravel application. Let’s see how can you add a few things in .htaccess file and easy to redirect the app http to https.

Navigate to your laravel root directory and find .htaccess file inside the public directory. After that open .htaccess file and update the following codes into it:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Or you can add the below for force redirect http to https using htaccess in laravel applicaiton.

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I hope this will work for you.

Leave a Comment