Forum Moderators: phranque

Message Too Old, No Replies

Virtual domains - canonical rewrite help

rewrite rules to create canonical URLs

         

ToyGuy

4:13 am on Feb 7, 2011 (gmt 0)

10+ Year Member



Need some help with httpd-vhosts file and rewrite rule application, please.

We have two issues. I would like ALL 60 virtual domains currently hosted (on one IP) to redirect permanently (for indexing purposes) to the www.domainname.com URL for each, rather than allowing the variation without the www subdomain. We do use some other subdomains for specific purposes, such as forum.domainname.com and demo.domainname.com, so the www or lack thereof should not affect every subdomain.

Secondly, we could consolidate several of the virtual domains into one set of instructions as they all point to the same location:

main site: [mydomain.com...] is the key site to log and index, other domains are:
www.mydomain1.com, www.mydomain2.com and www.mydomain3.com. Can we use rewrite rules for these too?

And we have a domain where we own the .org, .com and .net versions of the domain, but would like to consolidate these as well and have all point to www.domainname.org. In this case the "domainname" is identical.

Is this possible and will there be any substantial hit on the server resources as a result?
Thanks, Stephen

jdMorgan

10:33 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Order your redirects from "most complicated, more dependencies" to "least-complicated catch-alls" for a start.

Other than that, it's simply a matter of detecting root domain accesses and pre-pending "www"

# Catch-all TLD-to-www-subdomain redirect
RewriteCond %{HTTP_HOST} ^([^.]+\.(com|org|net))\.?(:[0-9]+)?$
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

The above may not make sense unless you take into account that "www" is also a subdomain...

This rule also removes non-canonical FQDN tags and port numbers appended to the hostname.

This rule is for use in httpd.conf (or similar config file) outside of any <Directory> containers.

Jim