On this page
A person holding a card with a binary code in his hand. In the background there is an inscription "www to non-www or non-www to www redirection".

Redirecting to www or non-www using Apache htaccess

Learn how to redirect to www or non-www using Apache htaccess for SEO and URL consistency.

Redirecting the domain to a www or non-www URL can help with URL canonicalization by search engines – the process by which a search engine selects which version of your site’s URL to display in search results.

By normalizing it (either having a www or non-ww URL), you will be able to control more about which URL shows up in search results by redirecting to a specific version of your website. According to Google Webmaster Guidelines, Bing Webmaster Tools or Yandex Webmasters, this may also help to reduce potential page rank lowering if the search engine is unable to determine which version of your URL is preferred.

One way to do that is to use the Apache htaccess configuration file and its mod_rewrite module.

Using .htaccess to redirect to www or non-www

There will be two versions: from www to non-www and from non-www to www. All universal.

Redirect from www to non-www

Redirect from www to non-www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,E=HTTPS:1]
</IfModule>

Here’s a breakdown of what each line does:

  • <IfModule mod_rewrite.c>: This line checks if the mod_rewrite module is enabled on the Apache server. If it is, the code block will be executed. If not, the server will ignore this block.
  • RewriteEngine On: This line enables the rewrite engine, which is necessary for the rewrite rules to work.
  • RewriteCond %{HTTP_HOST} ^www\. [NC]: This line is a condition that checks if the requested host (domain name) starts with www.. The ^ symbol indicates the start of the string, and \. is used to escape the dot character, which is a special character in regular expressions. The [NC] flag makes this condition case-insensitive, meaning it will match www, WWW, etc.
  • RewriteCond %{HTTP:X-Forwarded-Proto} !https: This line is another condition that checks if the request was not made over HTTPS. The ! symbol negates the condition, so this line matches requests that are not HTTPS. The X-Forwarded-Proto header is used to identify the protocol (HTTP or HTTPS) used by the client to make the request.
  • RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,E=HTTPS:1]: This line defines the actual rewrite rule. It matches any request (.*) and redirects it to the same host and URI, but over HTTPS. The https://%{HTTP_HOST}%{REQUEST_URI} part constructs the new URL by combining the HTTPS protocol, the host name (%{HTTP_HOST}), and the original request URI (%{REQUEST_URI}). The [R=301,L,E=HTTPS:1] part specifies that this is a permanent redirect (R=301), that this rule should be the last one processed if it matches (L), and that an environment variable HTTPS should be set to 1 for the duration of the request.
  • </IfModule>: This line marks the end of the IfModule block.

This code ensures that all requests to a website that are not already using HTTPS and that start with www. are redirected to the HTTPS version of the same URL without www..

Redirect from non-www to www

Redirect from non-www to www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,E=HTTPS:1]
</IfModule>

The difference in the code, in comparison to www to non-www redirection, is in two lines:

  • RewriteCond %{HTTP_HOST} !^www\. [NC]: This line is a condition that checks if the requested host does not start with www. (the ! negates the condition, meaning it checks for hosts that do not start with www.). The [NC] flag makes the comparison case-insensitive.
  • RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,E=HTTPS:1]: This line defines the actual rewrite rule. It matches any request ((.*)) and redirects it to the same host, but with https://www. prepended to the host name and the original request URI (%{REQUEST_URI}). The [R=301] flag indicates that this is a permanent redirect, which is good for SEO purposes. The [L] flag means that if this rule is applied, no further rules should be processed. The [E=HTTPS:1] sets an environment variable HTTPS to 1, which can be used in subsequent processing to indicate that the request was redirected to HTTPS.

Related posts

Comments

Leave a Reply

Real-user monitoring for Accessibility, Performance, Security, SEO & Errors (SiteLint)