Welcome to WebmasterWorld Guest from 23.22.79.235

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Unusual 301 rewrite with multiple TLDs

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

5+ Year Member



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)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

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

5+ Year Member



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)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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?

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

5+ Year Member



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)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim