Bernard Savonet
asked on
Problem with mod_rewrite when no arguments
My config is the following:
- DNS defines sub1.domain.com to point to the same IP address than domain.com
- on the server all this points to directory /var/www/
- I want all queries like http://sub1.domain.com to /var/www/data/zzz.html
- in /var/www is .htaccess in which contains:
- this works fine with URLs like http://sub1.domain.com?xx or http://sub1.domain.com/xx, it redirects as expected to http://domain.com/data/zzz.html
- it fails with URLs like http://sub1.domain.com or http://sub1.domain.com/, apparently in an endless loop (which luckily is stopped after 10 iteration).
How should I change my Cond and Rule?
- DNS defines sub1.domain.com to point to the same IP address than domain.com
- on the server all this points to directory /var/www/
- I want all queries like http://sub1.domain.com to /var/www/data/zzz.html
- in /var/www is .htaccess in which contains:
RewriteEngine on
Options FollowSymLinks
#RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
#RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L]
Problem:- this works fine with URLs like http://sub1.domain.com?xx or http://sub1.domain.com/xx, it redirects as expected to http://domain.com/data/zzz.html
- it fails with URLs like http://sub1.domain.com or http://sub1.domain.com/, apparently in an endless loop (which luckily is stopped after 10 iteration).
How should I change my Cond and Rule?
ASKER
(Oops.. I realize that when pasting code here I indeed forgot to remove the # in lines 4 & 5)
I am also considering the virtual host track, but other elements not presented here might make it impossible. So I would like first to stick with this "simple" rewrite that makes me mad.
Back to your suggestion: not sure why the !, since it is really sub1.domain.com I want to redirect
RewriteEngine on
Options FollowSymLinks
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L]
Thx for your suggestions.I am also considering the virtual host track, but other elements not presented here might make it impossible. So I would like first to stick with this "simple" rewrite that makes me mad.
Back to your suggestion: not sure why the !, since it is really sub1.domain.com I want to redirect
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx, that solves the problem.
The trick is the [R].
I was wrongly assuming that the rewriterules create a redirect... it does not if there is nothing to rewrite, unless it is told so thru the R which creates a redirect in all cases.
I am using [R] or its equivalent [R=302] because I want the original page to be indexed by spiders, which it would not if there was a redirect 301
So the final code will be
The trick is the [R].
I was wrongly assuming that the rewriterules create a redirect... it does not if there is nothing to rewrite, unless it is told so thru the R which creates a redirect in all cases.
I am using [R] or its equivalent [R=302] because I want the original page to be indexed by spiders, which it would not if there was a redirect 301
So the final code will be
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L,R]
No if you specify domain part it is P by default which is proxy. Proxy does not change host header so you get redirected over and over. Redirect in other hand changes host header.
ASKER
Thx.
Not only my problem was solved, but I have learned new tricks!
Not only my problem was solved, but I have learned new tricks!
RewriteCond %{HTTP_HOST} !^sub1\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L]
(notice exclamation mark reversing condition for rewrite.
Or you can set up 2 virtual hosts with different domains in different directories and only write rewrite rules for one. THis is what i would do cause its cleaner.