App Web Tutorials
  • Home
  • Contact
  • Privacy Policy
Reading: Master Laravel: The Ultimate Guide to .htaccess Redirects for WWW, Non-WWW, HTTP, and HTTPS
Share
Subscribe Now
App Web TutorialsApp Web Tutorials
Font ResizerAa
Search
  • Home
  • Contact
  • Privacy Policy
Follow US
Copyright © 2017-2025 App Web Tutorials. All Rights Reserved.
httpsLaravel 9sslwww

Master Laravel: The Ultimate Guide to .htaccess Redirects for WWW, Non-WWW, HTTP, and HTTPS

Chetan
By Chetan
Last updated: January 21, 2025
2 Min Read
Laravel .htaccess www, non-www,http and non-https redirection
Laravel .htaccess www, non-www,http and non-https redirection
SHARE

In this turorial we will create a redirection of all non secure request to https://www and https://non-www type url for Laravel Projects.

For the SEO purpose most people prefer all the canonical URL to be start with “https://www”, but we will cover both type of secure URL redirection in this tutorial.

For this to work you need create a .htaccess file in the public directory of your laravel project.

In the .htaccess file place the following code as it is to redirect all non secure URL to secure https://domainname.com/


    RewriteEngine On
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .	
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]	
    # Redirect Trailing Slashes If Not A Folder...	
    RewriteCond %{REQUEST_FILENAME} !-d	
    RewriteCond %{REQUEST_URI} (.+)/$	
    RewriteRule ^ %1 [L,R=301]	
    RewriteCond %{HTTPS}  !=on	
    RewriteRule ^/?(.*) https://domainname.com/$1 [R,L]	
    RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]	
    RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]	
    # Send Requests To Front Controller...	
    RewriteCond %{REQUEST_FILENAME} !-d	
    RewriteCond %{REQUEST_FILENAME} !-f	
    RewriteRule ^ index.php [L]

In the .htaccess file place the following code as it is to redirect all non secure URL to secure https://www.domainname.com/


RewriteEngine On

# Start with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

If you find this tutorial helpful please share it with others too.

Thanks

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article How to send Emails using SMTP in Laravel 9 from localhost and live server
Next Article Step by Step Tutorial to Convert HTML to PDF using JavaScript How to Instantly Convert HTML to PDF: A Foolproof JavaScript Guide for Beginners
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

FacebookLike
XFollow
InstagramFollow
Most Popular
How to Install and Use pyenv on Windows, Ubuntu, and macOS: A Complete Guide
The Ultimate Guide to Installing pyenv on Windows, Ubuntu, and macOS
January 21, 2025
How to Host a Python Flask App with SSL on WAMP Server in Windows: A Step-by-Step Guide for Beginners
January 21, 2025
Fixing SSH ‘Unprotected Private Key File’ Error in Windows 11: A Quick Guide
Easily Fix the SSH ‘Unprotected Private Key File’ Error on Windows 11 in Minutes
January 20, 2025
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
Power Your HTML Website with Stunning Animations: Easy Guide for Beginners
January 15, 2025
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
January 21, 2025

You Might Also Like

Laravel 9Laravel Email TemplatesSMTP Emails

How to send Emails using SMTP in Laravel 9 from localhost and live server

6 Min Read
Step-by-step tutorial on how to handle a 404 error and display a custom error page in Laravel
custom pagehandle 404 errorLaravel 9step-by-step tutorial

Step-by-step tutorial on how to handle a 404 error and display a custom error page in Laravel

2 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
App Web Tutorials

We provide tutorials, tips, tricks, and advice for improving programming and development skills.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?