Forum Moderators: phranque

Message Too Old, No Replies

Forcing removal of www AND redirecting from .co.uk to .com?

         

christopherwoods

4:51 pm on Aug 16, 2009 (gmt 0)

10+ Year Member



Once again, my insubstantial knowledge of regexp is letting me down (and Google seemed to have nothing similar to my query to help).

I'm playing with a site, and I've registered the name as a .co.uk and .com. The primary domain is .com, and .co.uk is configured as an alias on the host (using DirectAdmin).

I'm trying to accomplish two things:

1) redirect .co.uk to .com
2) remove the www for canonical purposes

What I have so far works fine for [domain.co.uk...] (or [domain.co.uk)...] to [domain.com,...] but [domain.com...] doesn't redirect to [domain.com....] I've tried all sorts but I'm not having much joy - either I can strip www from one domain fine, but arranging the condition rules linearly doesn't seem to work when handling more than one domain suffix.

These are the contents of my .htaccess:


Options +FollowSymLinks
RewriteEngine On

# Step 1: remove the www...

RewriteCond %{HTTP_HOST} ^(www..$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301]

# Step 2: redirect .co.uk to .com

RewriteCond %{HTTP_HOST} ^(www\.)?wheresmywispa\.co.uk [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?wheresmywispa\.co.uk [NC]
RewriteRule ^(.*)$ http://wheresmywispa.com/$1 [R=301,L]

The first part of that code is 'borrowed' from another thread on here, but I can see it's not interfacing correctly. Where am I going wrong? I'd like to learn what I'm missing, so I don't make the same mistake again. Is it possible to achieve this combined canonical rewrite and redirect within one rule, or would two rules be required (as I've attempted to do it above)?

Thanks in advance...

g1smd

5:00 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove Step 1 completely.

For Step 2, replace all of your conditions with just this one single condition:

# Redirect anything that is not EXACTLY example.com
RewriteCond %{HTTP_HOST} [b]![/b]^example\.com$

The ! means NOT. The ^ and $ are VITAL. Do NOT omit them.

Retain the same Rule that you already have.

This will redirect anything:
- with .co.uk in it
- with a www, on any TLD
- with a port number, from any domain
- in fact, anything that is not *EXACTLY* example.com

With a redirect you should get from original URL to final URL in just one hop. You must avoid a redirection chain.

.

*** "...combined canonical rewrite and redirect..." ***

You haven't got any rewrites there. You had two redirects. You need just one redirect.

You don't need any rewrites at all.

christopherwoods

8:31 pm on Aug 16, 2009 (gmt 0)

10+ Year Member



:O wow. So simple!

I'll give you an update; I've subsequently added some bits and bobs to my .htaccess to do some quick and dirty SEF rewriting for my URLs (hence my earlier mention of rewriting although I didn't include any).

Here's my current .htaccess (without any of your revisions):


Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www..$) [NC]
RewriteRule ^ [%{HTTP_HOST}%{REQUEST_URI}...] [R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.uk [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.uk [NC]
RewriteRule ^(.*)$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) [mysite.com...] [R=301,L]

## Hide dynamic URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?slug=$1

So, am I correct in understanding that this:


RewriteCond %{HTTP_HOST} !^mysite\.com$
RewriteRule ^(.*)$ /$1 [R=301,L]

is what you're suggesting? I'll give it a go, and it'll be interesting to see how DirectAdmin handles the wildcard redirect (this second domain isn't configured via a VirtualHost entry as I don't have root access to this box).

Out of curiosity, the way I was doing it (which almost-worked-but-not-quite) - where was I falling over in my syntax? Were the conditions just in the wrong order? Remember from way back how rules should be in the order of most-specific to least-specific (a rule which I think I broke myself earlier!)

christopherwoods

8:33 pm on Aug 16, 2009 (gmt 0)

10+ Year Member



Ack, I can't edit posts.

Correction: should the .co.uk/com redirect fix be as below?


RewriteCond %{HTTP_HOST} !^mysite\.com$
RewriteRule (.*) http://mysite.com/$1/ [R=301,L]

g1smd

8:54 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Almost. Remove the trailing slash from the target URL in that very last line of the redirect.

I'll ignore the post above that (as it contains a lot of errors and none of my corrections), except to say that redirects should be listed before rewrites and that lines 3 to 10 can be deleted and replaced by the single line of code I supplied.

g1smd

8:55 pm on Aug 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can edit posts for about an hour after posting. The edit button is below your name on the left.

christopherwoods

10:04 pm on Aug 16, 2009 (gmt 0)

10+ Year Member



Oh yeah! I never noticed that <hides>

I did notice some errors and duplicate rules in the stuff I posted, which is mostly due to me cobbling bits together from my old .htaccesses from other sites and scraping useful off the web. If I'd realised I could've edited the post, I would've done so ;)

Time to go jump into the sandbox again... I'll update if/when I get anywhere. Thanks for the pointers :)

jdMorgan

4:50 am on Aug 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your third rule-set above can be more-compactly (and -correctly) written as:

# Add missing trailing slash if final URL-path-part does not contain a period or slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ http://example.com/$1/ [R=301,L]

Note that this rules should precede your generic domain canonicalization redirect, since it is more-specific.

In order to avoid chained (multiple) redirects and avoid having external redirects 'expose' internally- rewritten filepaths to the client, put your rules in order with external redirects first, ordered from most-specific (fewest URLs affected) to least-specific, followed by internal rewrites, again in order from most- to least-specific.

When using resource-intensive RewriteCond functions like filesystem checks (e.g. -d and -f) and reverse-DNS lookups, put those RewriteConds last when given a choice, and make the RewriteRule pattern as specific as possible (RewriteConds are not processed unless the RewriteRule pattern matches). In other words, make sure those CPU-killers are not executed unless absolutely necessary.

Jim