Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Canonicalization: best code to redirect no-www to www & index to /


ewwatson - 2:46 pm on Jan 13, 2013 (gmt 0)


Because I was originally getting no responce here I started a new thread over at sitepoint. I trust your guys opinion since you seem to know your stuff. A veteran over there gave me this solution. What do you think? Is it perfect or is it missing things? I know little to nothing about this stuff so I am at your kind mercy.

RewriteEngine on
# Send subdomains off to their own domains so
# you don't need to worry about exclusions below
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+\.))maindomain\.com$ [NC]
# Capture optional www and subdomain (better to list your subdomains inside (sub1|sub2|...) )
RewriteRule .? http://%1com{%REQUEST_URI} [R=301,L]
# Send {REQUEST_URI} to subdomain.com
# Note, this can also send to www.maindomain.com
# (if you don't list as recommended above) but
# that will be corrected below

# 301 permanent redirect index.html(htm) to folder with exclusion for addon domains
# Rephrased: Strip subdirectories from any index.htm(l) request
# RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com)
# Irrelevant
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
# {THE_REQUEST} is a terrible variable to use and you don't need to use the whole thing!
# In fact, you don't need to use it at all!
RewriteRule .+/index\.html?$ http://www.maindomain.com/index.html [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|websitecodetutorials\.com)
# Ditto the above
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule .? http://www.maindomain.com${REQUEST_URI} [R=301,L]


Thread source:: http://www.webmasterworld.com/apache/4508251.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com