How to redirect a website to https://www using .htaccess


To redirect a website to HTTPS://www using .htaccess, you can add the following code to your website’s .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

This code will redirect all HTTP traffic to HTTPS and force all URLs to include the www prefix.

Here’s a breakdown of the code:

  • RewriteEngine On: Enables the mod_rewrite module, which is required for URL rewriting.
  • RewriteCond %{HTTP_HOST} !^www\.: Checks if the current request does not include a www prefix.
  • RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]: Redirects all HTTP requests to the corresponding HTTPS URL with the www prefix and sets the HTTP status code to 301. The L flag tells Apache to stop processing the rewrite rules after this rule is matched.

Leave a Reply

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