Redirect non-www to www & HTTP to HTTPS using .htaccess file

.htaccess file is a configuration file used to override the main server configuration. .htaccess file placed in a directory and the configuration is applied to that particular directory and all the subdirectories. The most usage of .htaccess file is Rewriting URLs, Blocking, SSL, Customized error responses, Directory listing, Cache-Control, etc.

You can define the conditional URL rewrite rules using RewriteCond and RewriteRule in HTACCESS. This HTACCESS tutorial will show you the most used URL redirection process through the .htaccess file. At first, you’ll see how to redirect non-www URLs to www and the second one will show you how to redirect HTTP to HTTPS using a .htaccess file.

redirect-non-www-to-www-http-to-https-using-htaccess-file-by-codexworld

Create .htaccess File

Create a file with .htaccess extension in the root directory of the website domain. It will be a simple text file and you can edit this file with any text editor.

Redirect non-www to www URL

As per the Search Engine prospect, non-www and www URLs are different and it could affect website SEO. For example, http://codexworld.com and http://www.codexworld.com are technically different in Search Engines. When a Search Engine fetches the same content from different URLs, it is treated as duplicate content. So, it will be a good idea if you redirect all requests (non-www) to the same URL format (www). Using the .htaccess file, you can easily redirect non-www to www URL.

Add the following code in the root .htaccess file to redirect the non-www to www URL.

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

Redirect HTTP to HTTPS URL

If the secure certificate (SSL) was installed on your website, you can redirect visitors to the secure web version. Sometimes SSL encrypted connection is necessary for your website. This means you would like to force website visitors to use SSL by automatically redirecting HTTP to HTTPS version of the URL.

Add the following code in the root .htaccess file to redirect the HTTP to HTTPS URL.

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

Redirect HTTP & non-www to HTTPS & www URLs

It’s always been a good idea to maintain the same URL format for all the requests. To fulfill this purpose, redirect non-www and HTTP requests to HTTPS and www formatted URLs.

The following HTACCESS code snippet redirects all HTTP & non-www requests to HTTPS & www URL.

RewriteEngine On 

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

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

Here, we have discussed the most used rewrite rules and redirect URLs using HTACCESS. If you need any help regarding the .htaccess file, feel free to post your comment below.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

RELATED TUTORIALS

13 Comments

  1. Peter Said...
  2. Muhammad Arslan Said...
  3. Bryson Frantz Said...
  4. Amit Kumar Said...
  5. Rali Dimitrov Said...
  6. Uresh Said...
  7. Wiki Said...
  8. Tery Said...
  9. Pooja Khokhani Said...
  10. Ravi Kumar Sharma Said...
  11. Sahar Said...
  12. Fathaq Said...
  13. Relaxtronica Said...

Leave a reply

keyboard_double_arrow_up