Web Hosting Talk







View Full Version : Problem with .htaccess and mod_rewrite


ramirez
07-27-2005, 09:59 AM
Hello, I am not sure if this is the fitting place for this, so feel free to move this if it's the wrong place.

I am having a small problem with mod_rewrite Apache module.
I am trying to setup a rather simple vhost using mod_rewrite (I have already setup the DNS), since I don't have access to the httpd.conf
I have this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?esper\.avalanchestudios\.net$
RewriteRule (.*) omega/sites/esper.avalanchestudios.net/$1 [L]
The problem with this is that since the HTTP_HOST doesn't change after the rewrite, when the new URI is rewritten, the RewriteCond again matches, thus this results into an unlimited loop and furthermore to an invalid setup.
I want to know the best way to solve this problem, I did come up with a small fix myself, but I find it an ugly workaround, and would much rather use a better solution, if there is any.
My solution was to make another condition for the rewrite, that checks if the REQUEST_URI is in the directory of the document root. If it is, it won't rewrite the URL again:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?esper\.avalanchestudios\.net$
RewriteCond %{REQUEST_URI} !^/omega/sites/esper\.avalanchestudios\.net/
RewriteRule (.*) omega/sites/esper.avalanchestudios.net/$1 [L]
The problem with this is that if I happened to have a same named directory inside the rewritten docroot, it wouldn't work. Yes, unlikely, but possible.

So anyways, if you have any better ideas, please let me know. (Also, I am going to turn this into map file later to support multiple vhosts easily, but I am just trying to setup the base framework now).

PerfTuner
07-27-2005, 06:08 PM
So anyways, if you have any better ideas, please let me know.
Adding NS (which stands for no-subrequest) flag to rewrite rule prevents loops like this.
RewriteRule (.*) omega/sites/esper.avalanchestudios.net/$1 [L,NS]

ramirez
07-27-2005, 06:35 PM
I did this as you suggested:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?esper\.avalanchestudios\.net$ [NC]
RewriteRule (.*) omega/sites/esper.avalanchestudios.net/$1 [L,NS]
However, this resulted into an error (Internal Server Error).
Did I do something wrong?

PerfTuner
07-27-2005, 07:16 PM
Hm. No idea. :( It's been ages since I've worked without having access to httpd.conf, so I may be wrong. I think you'll have to keep ugly %{REQUEST_URI} !^/omega... stuff.