Forum Moderators: phranque

Message Too Old, No Replies

Simple mod_rewrite rule doesn't work

Complex redirection worked fine

         

geekay

10:23 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



I tried to simplify and clean up my htaccess file, but now one simple rule doesn't work anymore:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteRule (.*) [domain1.com...] [R=301,L]

It worked fine before. Could anybody please tell me why those lines don't work when standing alone? I can't understand how the removal of the previous lines interfer.

It's a case with more than one domain on the same IP. I try to get domain1 to always redirect to domain1/folder1/. The other domains do not redirect in this way and are OK.

The previous lines were, in case someone is interested:

RewriteRule ^folder2¦foolder3 - [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain2\.com [NC]
RewriteRule ^folder1 - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com [NC]
RewriteRule (.*) [domain3.com...] [R=301,L]

Folder1, 2 and 3 are all under domain1.

jdMorgan

11:33 pm on Nov 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These two lines prevented your old /folder1 code from looping. They prevented /folder1 URLs from being rewritten to /folder1/folder1, etc.

RewriteCond %{HTTP_HOST} !^(www\.)?domain2\.com [NC]
RewriteRule ^folder1 - [L]

In order to prevent looping of your new rule, you'll need to add a RewriteCond:

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

In addition, if you don't want the "/folder1" to show in the browser, use this form:

RewriteCond %{REQUEST_URI} !^/folder1
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteRule (.*) /folder1/$1 [L]

Jim

geekay

7:07 am on Nov 29, 2004 (gmt 0)

10+ Year Member



Thank you so much, Jim! That solved my problem. One short additional question, if at all possible? The following code is my own invention and seems to work as it should, but I'm worried about hidden bugs.

RewriteCond %{HTTP_HOST} ^(www\.)?domainY\.com [NC]
RewriteRule ^folder1 [domainX.com...] [R=301,L]

In the previous rule (not shown here) all requests for a domainZ are first redirected to domainY. (Z actually represents more than one now parked domain.)

But, if the request as an exception was for domainZ/folder1/, then it is to be served as domainX/folder1/. (Folder1 is intended to exist only under domainX, but there is some SE mess.) It's a double redirection and only occasionally needed.