Archives – May 10, 2006

Apache Rewrite Condition for Subdomain

Today I added a new rewrite condition to my apache configuration. This rewrite condition would forward any invalid subdomain request to the correct subdomain. So for example a user type in their url box ww.domain.com or any other wild-card subdomain, the user should be forwarded to the default www.domain.com.

A situation might occur like this. A person who likes your page, make a link to your page, due to human error, the person types ww.domain.com instead of www.domain.com. This creates two subdomains with identical pages, which is a no no for the search engines.

One of the advantages to forward invalid subdomain to the default is that this is a concept widely practiced to suit the search engine optimization for both google and yahoo. According to my research, some of the search engine crawlers dislikes multiple identical copies of pages from different domains or subdomains. It is known that if one to disobey the search engines algorithm, then their pages might get penalized and removed from the search results, which is not good. Here is how I add the rewrite condition to my apache configuration to forward any invalid subdomains to the default www.domain.com.

Add the following lines to your apache configuration:
ServerAlias domain.com www.domain.com *.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule (.*) http://www.domain.com$1 [R=301,L]

Notice the return 301 status code which tells the requester that the invalied subdomain has moved permanently. This again is search engine friendly.

Leave a Comment May 10, 2006


 

May 2006
M T W T F S S
« Apr   Jul »
1234567
891011121314
15161718192021
22232425262728
293031  

Pages

Blogroll

Categories