开发者

mod_rewrite: redirect to a folder with the same name as domain

I have a bunch of domains pointing to the same folder, the root of my host "public_html". I wish I could redirect them, each one for a folder with the same name as the domain.

eg: redirect www.mydomain.com to "public_html/www.mydomain.com"

I tried this:

开发者_如何学Python
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/.*$
RewriteRule ^(.*)$  /%{HTTP_HOST}/$1 [L]

But with no success. What's wrong? Its even possible to redirect to folder that uses a 'dot' on its name?

Thanks in advanced.


The test pattern in your RewriteCond is actually trying to match input starting with the literal string /%{HTTP_HOST}/. The value of the %{HTTP_HOST} variable is not expanded like you were expecting, so the condition will always be true (the pattern itself will never match). You'll need to modify the RewriteCond, and there are a few different approaches.

If this is the only rewrite you perform, you can just check to see if you've done it yet:

RewriteCond %{ENV:REDIRECT_STATUS} =""

Or, if the resource won't exist until you rewrite it, you can check for that instead:

RewriteCond %{REQUEST_FILENAME} !-f

Finally, you can mimic your original condition by using backreferences within the test pattern, along with a separator character that won't appear in your URL:

RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1

Note that this doesn't guarantee that you've done the redirection though, it only ensures that the request URI has the host name as its first path segment. However, that's generally good enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜