mod_rewrite: multiple RewriteRules do not work well together

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

mod_rewrite: multiple RewriteRules do not work well together

Post by tomfra »

I have a "multiple RewriteRules" problem on a website using wildcard DNS.

Generally, any request for http://something.domain.com/file.html should be served from http://domain.com/MAIN_DIR/something/file.html . I am using the below code for this purpose (which has been written by someone else) and it has been working just fine.

Code: Select all

RewriteEngine on
RewriteCond   %{HTTP_HOST}                 ^[^.]+\.MYDOMAIN\.COM$ 
RewriteCond   %{HTTP_HOST}                 !^(www\.)?MYDOMAIN\.COM$ 
RewriteCond   %{REQUEST_URI}               !^/MAIN_DIR/ 
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^([^.]+)\.MYDOMAIN\.COM(.*) /MAIN_DIR/$1/$2
However, I have problems with search engine spiders which look for the robots.txt file in http://domain.com/MAIN_DIR/something/ and don't find it there. My apache log is full of page not found errors because of it and I am somewhat worried it may increase server load.

So I want to redirect all requests for http://something.domain.com/robots.txt (or actually http://domain.com/MAIN_DIR/something/robots.txt) to http://domain.com/robots.txt. I've tried this code in .htaccess so solve it:

Code: Select all

RewriteEngine on
RewriteCond   %{HTTP_HOST}                ^[^.]+\.%{HTTP_HOST}/robots.txt$
RewriteRule   ^/$                         %{HTTP_HOST}/robots.txt   [L,S=2]
This code does indeed work but it does not when used together with the other RewriteRules code I mentioned above. I have placed it before that code so my .htaccess looks like:

Code: Select all

RewriteEngine on
RewriteCond   %{HTTP_HOST}                ^[^.]+\.%{HTTP_HOST}/robots.txt$
RewriteRule   ^/$                         %{HTTP_HOST}/robots.txt   [L,S=2] 

RewriteCond   %{HTTP_HOST}                 ^[^.]+\.MYDOMAIN\.COM$ 
RewriteCond   %{HTTP_HOST}                 !^(www\.)?MYDOMAIN\.COM$ 
RewriteCond   %{REQUEST_URI}               !^/MAIN_DIR/ 
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^([^.]+)\.MYDOMAIN\.COM(.*) /MAIN_DIR/$1/$2
I thought the [S=2] flag would have helped but it has not. I have to admit I am not good at this stuff so I suppose I may have missed something.

Any help is very welcome!

Tomas
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: mod_rewrite: multiple RewriteRules do not work well toge

Post by Buddha443556 »

Code: Select all

RewriteEngine on
RewriteCond   %{HTTP_HOST}                ^[^.]+\.%{HTTP_HOST}/robots.txt$
RewriteRule   ^/$                         %{HTTP_HOST}/robots.txt   [L,S=2]
That doesn't look like it should work. %{HTTP_HOST} shouldn't contain the resource requested (in this case /robots.txt). Test string should probably be %{REQUEST_URI} and pattern should just be robots.txt.

Code: Select all

RewriteEngine on
RewriteCond   %{REQUEST_URI}                robots.txt$
RewriteRule   ^(.*)$                         $1   [L]
HTH
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

Thanks! That works!

Actually, I should have known that {HTTP_HOST} does not contain the whole string but for some reason I focused on another part of the code and was wondering why it didn't work.

Thanks again!

Tomas
Post Reply