Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 for parked domains

         

internetheaven

9:41 am on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anyone know how to set up the .htaccess file so that whichever web address is calling it all non-www versions get 301 redirected to the www. version?

I currently use this on all my sites:

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

but obviously that will only redirect example.com

[edited by: jdMorgan at 5:05 pm (utc) on Jan. 9, 2008]
[edit reason] example.com [/edit]

jdMorgan

5:05 pm on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Redirect all requests for any domain *except* www.example.com or <blank>

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

This code is posted in hundreds of threads here, but I guess once more won't hurt...

Jim

internetheaven

5:40 pm on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, guess I wasn't as clear as I could have been. The code you have given me is to redirect all requests to www.example.com, yes?

I don't want to redirect them all to www.example.com, that is easy enough to do - what I want is that when each domain is accessed without it's www. the user is redirect to the www. version of that domain

e.g. example1.com is called, it is redirected to www.example1.com
example2.com is called, it is redirected to www.example2.com
example3.com is called, it is redirected to www.example3.com

The parking means that they all share the same .htaccess file which is why I'm looking to do it from there.

Thanks
Mike

jdMorgan

8:09 pm on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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

Jim

[edited by: jdMorgan at 4:24 am (utc) on Jan. 10, 2008]

internetheaven

9:13 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wish I'd popped back sooner, I'd have seen your edit! Just spend hours trying to work out why it wasn't working then Mr Genius walks in (not his real name) and says "you've got a space missing next to the!" ...

Thanks though!
Mike

jdMorgan

10:34 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's an annoying problem with posting here: The board software deletes spaces preceding exclamation points, and special care has to be taken to work around that. Either double-spaces must be typed, or a markup tag must be inserted ahead of the exclamation point. Using some extra spaces to show the markup codes which would otherwise render, I often use
 RewriteCond %{HTTP_HOST} [ b ] [ / b ]!^www\.
-or-

RewriteCond %{HTTP_HOST} [ i ] [ / i ]!^www\.
to prevent the space from being removed.

Similarly, the forum software modifies the solid pipe character to a broken pipe "¦" character, and requires anyone copying-and-pasting code from this forum to hand-edit all instances of that character back to a solid pipe.

Sometimes I forget to use the space-eater work-around, and sometimes I forget to mention editing the pipe characters... <sigh>

Jim