Redirect sub-domain traffic with .htaccess

For our Tutorial Tuesday this week, we thought we'd cover off redirecting sub domain traffic using an .htaccess file. It's always worth remembering that Google views different sub-domains as different sites, so your SEO gain is going to waste if it is spread across two domains, for example, www.domain.com and domain.com.

 

For our Tutorial Tuesday this week, we thought we'd cover off redirecting sub domain traffic using an .htaccess file.

It's always worth remembering that Google views different sub-domains as different sites, so your SEO gain is going to waste if it is spread across two domains, for example, www.domain.com and domain.com.

Redirecting non-www traffic through to www

This is where a quick bit of trickery with your .htaccess file and Apache's mod_rewrite comes in:


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

Redirecting www traffic through to non-www

You might feel that the url is tidier without the www (we do here at Corvita), or you might need to point traffic through from an old sub-domain that no longer exists:


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

I don't have access to an .htaccess file

Just because you don't have access to your .htaccess file, this doesn't mean that you can't set up domain redirects. These can be set up within your application, but adding to an .htaccess file is definitely a quicker and tidier option.

January
January