Forum Moderators: phranque

Message Too Old, No Replies

Forcing www. in web address

Using .htaccess to force the full http://www.domain.com address

         

Red5

2:40 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



Dear all,

This is a snippet from my current .htaccess file:

# this ruleset converts .co.uk to .com
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co.uk [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ [domain.com...] [R=301]

What I'd like to do is to add a RewriteRule to force the inclusion of the www. in the address so that [domain.com...] translates to [domain.com....]

Could you help, please?

Thanks in advance. ;-)

jdMorgan

3:04 pm on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Red5,

Just [OR] in another RewriteCond:


RewriteCond %{HTTP_HOST} ^(www\.)?domain\.c[b]o\.u[/b]k [NC[b],OR][/b]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://www.domain.com/$1?%1 [R=30[b]1,L][/b]

The [OR] is local, and acts only between the first and second RewriteCond, so the resulting logic is:
IF ((the requested hostname is either of the .co.uk domains OR the domain.com domain) AND (any query string))
THEN do the rewrite to www.domain.com.

Note the other minor changes, too.

You shouldn't actually have to explicitly carry forward the query string -- This should happen automatically unless you explicitly clear the query string in your RewriteRule. So you might try just using:


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

Ref: Apache mod_rewrite documentation [httpd.apache.org]

Jim

Red5

3:22 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



Thanks Jim!

I opted for your streamlined version and it works perfectly. Thanks for your quick reply.

Regards,

Red5