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:
RewriteEngine on
Options FollowSymLinks

#RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
#RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L] 

Open in new window

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?
LVL 29
Bernard S.CTOAsked:
Who is Participating?
 
a1jCommented:
Ah yeh sorry - try this

RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [R=301,L]
0
 
a1jCommented:
You can either


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.
0
 
Bernard S.CTOAuthor Commented:
(Oops.. I realize that when pasting code here I indeed forgot to remove the # in lines 4 & 5)
RewriteEngine on
Options FollowSymLinks

RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L]  

Open in new window

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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Bernard S.CTOAuthor Commented:
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
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [L,R]   

Open in new window

0
 
a1jCommented:
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.
0
 
Bernard S.CTOAuthor Commented:
Thx.
Not only my problem was solved, but I have learned new tricks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.