homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Unusual 301 rewrite with multiple TLDs

 12:49 am on Feb 11, 2010 (gmt 0)

I'm trying to get my web sites on board with the whole "301 everything that doesn't start with www. to www" thing, but I'm having trouble.

I have multiple domains on various TLDs that all land in the same directory, and thus use the same .htaccess file.

The goal is to have traffic to example.at 301 to www.example.at and for traffic to example.co.uk to 301 to www.example.co.uk, etc... for .com and some other TLDs.

Based on my reading in this forum, I adapted a solution from another thread and thought this might work.

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+\.)+([^\.]+\.(com|co\.uk|info|at))\.?(:[0-9]+)?$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]

The good news is that it doesn't appear to cause any harm. The bad news is that it doesn't appear to do anything. When this is successfully installed, will the address in the browser's URL bar reflect the new "www" domain if I point it to a non-www domain? As it is now, it just stays with the old non-www domain.

[edited by: jdMorgan at 1:54 am (utc) on Feb 11, 2010]
[edit reason] Delinked [/edit]



 2:07 am on Feb 11, 2010 (gmt 0)

The code you copied requires a subdomain *other* than "www" in order to be invoked, as described in the thread where you found it. If you wish to redirect when there is no subdomain whatsoever, the first RewriteCond isn't needed, the second RewriteCond's pattern must be modified, and the back-reference in the RewriteRule must be changed:

RewriteCond %{HTTP_HOST} ^([^\.]+\.(com|co\.uk|info|at))\.?(:[0-9]+)?$
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L]

I'm also presuming that you already have other working rewriterules, and so already have the necessary mod_rewrite "setup" directives in place.

Further, you may wish to also redirect the "www" subdomains if the requested hostname is in FQDN format (i.e. has a trailing period), if it has a port number appended, or both:

RewriteCond %{HTTP_HOST} ^([^\.]+\.(com|co\.uk|info|at))\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.(com|co\.uk|info|at))(\.|\.?:[0-9]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

Note also the alternate and equivalent form of the RewriteRule in this example.



 1:44 pm on Feb 11, 2010 (gmt 0)

That first example worked perfectly, thanks. I don't do anything fancy with ports, so I'll just stick with the simpler solution.

Hopefully this will help with my Google 302 hijacking problems as outlined here: [clsc.net...] and if it doesn't, at least there's no harm done.


 3:01 pm on Feb 11, 2010 (gmt 0)

It matters not whether you "do anything fancy with ports." What matters is whether anyone else does when they link to you... How about if I point a few hundred links at your "www.example.com" web site like this: "www.example.com.:80/index.php?some-nonsense-query-here". Try that in your browser. Do you redirect that, or allow the duplicate content?



 6:46 pm on Feb 11, 2010 (gmt 0)

Upon further testing, it is interesting to notw that both of the solutions provided break my installation of phpMyAdmin. Go figure.


 8:48 pm on Feb 11, 2010 (gmt 0)

You may need to exclude the root directory of phpAdmin and anything below it by adding a negative-match RewriteCond to the rule; Their code may not be able to handle anything but "main-domain" accesses.

It's quite simple, once you know what the actual problem is -- both in initial rule design, and in addressing little gnats like this one.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved