Forum Moderators: phranque

Message Too Old, No Replies

301 redirect

Syntax Help

         

ControlZ

8:04 am on Nov 10, 2005 (gmt 0)

10+ Year Member



Attempting to add additional domains to a 301 redirect I already have written, but not sure how to write it.

RewriteEngine On

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

example.com is the domain I am attempting to redirect parked domains to. example2.net is a parked domain. I need to add several additional parked domains, but not sure of the syntax.

chirp

2:27 pm on Nov 10, 2005 (gmt 0)



The easiest way is to handle them verbosely:

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

Then you can look at grouping them with regular expressions if you want ;)

ControlZ

4:04 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



Thanks Chirp,

What does the [NC,OR] mean?

ControlZ

4:15 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



One additional question. How would I add additional lines to redirect domains entered without the www?

In other words I want to 301 http://example.com to www.example.com?

jdMorgan

4:27 pm on Nov 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[NC] = case-insensitive compare
[OR] = Logical 'or' with next RewriteCond (DO NOT include on last RewriteCond!)

The second RewriteCond above would have caused an 'infinite' redirection loop, because it would have redirected www.example.com to itself repeatedly. The fix for that ties in with your new question, and the answer is to remove the (www\.)? from that RewriteCond, and use it as shown here as the first RewriteCond:


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

I also removed unnecessary regex tokens, and removed the end-anchors from the domain names; Including them will cause your rule to fail if a user or proxy includes a port number, e.g. example.com:80

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

ControlZ

4:45 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



Thanks Jim,

Your fix seemed to work. All parked domains are redirecting BUT, something strange is happening. None of my css styles are working. Basically, I am looking at a html doc with no attached styles.

In case you are wondering, no I have not made any recent changes to any of the styles or pages.

ControlZ

4:53 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



Well, maybe I should try another day. The 301 seems to be screwing things up. Now when I enter any of the parked or main domain, I get the following error.

"Redirection limit for this URL exceeded. Unable to load requested page. This may be caused by cookies that are blocked"

Here is the 301 I set:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com [NC,OR]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

ControlZ

5:12 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



Got it -- seems I forgot to read your rule about not including [OR] after the last RewriteCond. Changed it to [NC]from [NC,OR] and everything is working as it should be.

I am new to Apache as if you could not tell. Where is the best place to learn what all the little files on the server are. For example, .bash_profile?

jdMorgan

5:42 pm on Nov 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whenever I find any file that I can't identify or any error strange message from any program, my first stop is usually my favorite search engine, where I enter it (or most of it) as a quoted search. This is very handy for finding out whether an executable file is a trojan, as well as identifying the program associated with, and the cause of, obscure error messages.

Getting used to mod_rewrite takes some time. But you'll find it worth the investment... ;)

Jim