Configuring redirections with apache htaccess for www, non-www, http and https | All patterns

Configuring redirections with apache htaccess for www, non-www, http and https | All patterns

There could be 10 different reasons to have a specific pattern of domain name considering brand value, technical architecture, personal liking, search engine optimizations, etc. And you might encounter a requirement of http redirections where you may need to do add/edit the htaccess of your webserver.

 Understand, this blog is just to help you achieve the results and not about explaining each bit of code. 

Prerequisites

  • This tutorial assumes that you have enough technical knowledge about setting up a website on apache.
  • If you are using Linux make sure you have superuser privileges, i.e. sudo or root, on the server that is running Apache.
  • It is assumed that you have Apache installed.
  • You must be able to add records to the DNS that is managing your domain. If you do not already have a domain, you may purchase one from a domain registrar, and manage it with the registrar’s DNS.
  • Enable Rewrite Module: In order to perform the 301 redirect, we will use the Apache mod_rewrite, or Rewrite, module. Doing so will ensure that your users can access your site with or without the www. prefix, and be redirected to the domain that you prefer.
  • Created an .htaccess file on the web server root folder for http redirections.

Apache htaccess configurations for http redirections from www to non-www and vice-versa with https options on/off.

If you are reading this post, it means you are technical enough to understand about this topic. So let me quickly jump to specific solutions to different requirements:

Condition 1: To get www without https

Example:

  1. http://example.com ⇨ http://www.example.com
  2. https://example.com ⇨ http://www.example.com
  3. https://www.example.com http://www.example.com

Result: http://www.example.com

Solution:

RewriteEngine On

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

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

Condition 2: To get www with https

Example:

  1. http://example.com ⇨ https://www.example.com
  2. https://example.com ⇨ https://www.example.com
  3. http://www.example.com ⇨ https://www.example.com

Result: https://www.example.com

Solution:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Condition 3: To get non-www with https

Example:

  1. http://www.example.com ⇨ https://example.com
  2. https://www.example.com ⇨ https://example.com
  3. http://example.com ⇨ https://example.com

Result: https://example.com

Solution:

RewriteEngine on

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

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

Condition 4: To get non-www without https

Example:

  1. http://www.example.com ⇨ http://example.com
  2. https://www.example.com ⇨ http://example.com
  3. https://example.com http://example.com

Result: http://example.com

Solution:

RewriteEngine on

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

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

Conclusion

That’s it! Your .htaccess redirect is now configured, and your users will be able to access your web server with the configured url pattern.

If you enjoyed the article, please share it!

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments