Published: 8th Mar 2018 | Words: 429 | Reading Time: 3 minutes


.htaccess www Redirects

Properly configured website redirects are a key aspect of SEO. Using .htaccess you can redirect traffic from variations of your domain to your main / correct one. It doesn't matter what domain you can use, you can easily redirect other variations to the correct one and its extremely important that redirects are set up properly otherwise you can end up with a lot of duplicates. Remember to verify all the variations of your websites in webmaster tools so you can see if there is traffic going to a variation that you don't want traffic going to. The rules here perform a 301 redirect, which is permanent redirect and should be used for variation domain redirects.


Adding a www to non www redirect

Today its not really necessary to have www. in your domain. The only real thing it does is make it harder to type your domain. If your launching a new website on a new domain your best off sticking with a non www version of it. You can easily redirect to the none www version by checking does the hostname start with it.

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

The first line here sets the rewrite base. This is optional and if you are using it, it only needs to be set once at the top of your .htaccess file.

The second line uses regular expression to check the hostname and see if it starts ^ with www., the back slashes are to escape the dots because these have special meaning in regular expression.

The third line takes the entire path (.*) and appends it as $1 after the protocol + hostname we are redirecting to.

If you don't use rewrite base this rule will be slightly different ^/(.*)$


Adding a non www to www redirect

Both redirect rules are fairly similar, one is just the opposite of the other really. You can redirect to the www version by checking if the current host does not start with www.

RewriteBase /
RewriteCond %{HTTP_HOST} ^ddmseo\.com$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

The rules here are extremely similar to the one above. The only real difference is the second line. Instead of checking for the www version of the domain, we check for the non www version.