[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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?
0
fibo
Asked:
fibo
  • 3
  • 3
1 Solution
 
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
 
fiboAuthor 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
 
a1jCommented:
Ah yeh sorry - try this

RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteRule ^(.*)$ http://domain.com/data/zzz.html [R=301,L]
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
fiboAuthor 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
 
fiboAuthor Commented:
Thx.
Not only my problem was solved, but I have learned new tricks!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now