Quick .htaccess question

Associate
Joined
29 May 2003
Posts
2,038
Location
Cambridge
For a site we manage and for which we have the .com and .co.uk variant of the domain name, I've been asked to ensure that the site pages are viewable whether the user types the www bit in the location bar of their browser.

Say the actual site files were hosted under:

Code:
http://www.site.com

We'd want

Code:
http://site.com

OR

http://www.site.co.uk

OR

http://site.co.uk

to show the same files and change the URL in the location bar to http://www.site.com

If needs be the 'with the www' option can be handled easily via Plesk running on the hosting server - just a matter of setting up standard rather than frame forwarding. Can't see any way for it to handle 'without the www' though.

To cope with the 'no www' option, I suspect this will need to be done with an .htaccess file, but I confess I'm a bit of a novice with these - I've only ever used them thus far for preventing the listing of a directory. I've just been poking around on Google for the last hour or so and found lots of examples, but nearly all are talking about forwarding specific directories and such like, rather than an entire domain with or without a www prefix like I'm needing to do here.

Any suggestions as to how I can go about this? I don't doubt for a second what I'm needing to do is utterly simple, but I'm wary of stumbling in with only half a grasp of what I'm doing and fouling things right up!
 
Excellent - thanks for that!

Just to make sure I've got this straight, my .htaccess file will need:

Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

to handle the .com and then I'll need to put another block underneath it in the same .htaccess file that reads like this:

Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]

to handle the .co.uk variation, yes?
 
Well you don't need to repeat the first two lines. I actually just tested that and it didn't work, possibly because the dots weren't escaped with backslashes, but the following is tested and works:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

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

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

Edit: No problem. Just edited it because I noticed the forum changed what I put. I am guessing its because of the php tags I put round it so I have just taken them out.
 
Last edited:
Back
Top Bottom