开发者

Using mod-rewrite to conditionally select existing file in a subdirectory based on Host header?

I'm working through a problem where I want to select a different static content file based on the incoming Host header. The simple example is a mapping from URLs to files like this:

  • www.example.com/images/logo.gif -> \images\logo.gif
  • skin2.example.com/images/logo.gif -> \images\skin2\logo.gif
  • skin3.example.com/images/logo.gif -> \images\skin3\logo.gif

I have this working with the following RewriteRules, but I don't like how I have t开发者_开发问答o repeat myself so much. Each host has the same set of rules, and each RewriteCond and RewriteRule has the same path. I'd like to use the RewriteMap, but I don't know how to use it to map the %{HTTP_HOST} to the path.

<VirtualHost *:80>
    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
    ServerName www.example.com
    ServerAlias skin2.example.com
    ServerAlias skin3.example.com

    RewriteEngine On

    RewriteCond %{HTTP_HOST} skin2.example.com
    RewriteCond %{DOCUMENT_ROOT}$1/skin2/$2 -f
    RewriteRule ^(.*)/(.*) $1/skin2/$2 [L]

    RewriteCond %{HTTP_HOST} skin3.example.com
    RewriteCond %{DOCUMENT_ROOT}$1/skin3/$2 -f
    RewriteRule ^(.*)/(.*) $1/skin3/$2 [L]
</VirtualHost>

The concept behind the rules is if the same filename exists in a subdirectory for that host, use it instead of the direct targeted file. This uses host based subdirectories at the lowest level, and not a top level subdirectory to separate content.

Update: I want to revise the question to assume the hostname doesn't match the subdirectory exactly, but does exist as a 1-1 map. Here would be the revised URL to file mapping.

  • www.example.com/images/logo.gif -> \images\logo.gif
  • skin2.example.com/images/logo.gif -> \images\s2\logo.gif
  • skin3.example.com/images/logo.gif -> \images\s3\logo.gif

For use with RewriteMap, I created this data file:

##  hostmap.txt -- hostname to subdirectory map
skin2.example.com    s2   # map skin2 to s2 directory
skin3.example.com    s3   # map skin2 to s3 directory

And changed the RewriteRules to use %{HTTP_HOST} directly in the map call, which seems to be working.

RewriteEngine On
RewriteMap hostmap txt:conf/hostmap.txt 
RewriteCond %{DOCUMENT_ROOT}$1/${hostmap:%{HTTP_HOST}}/$2 -f
RewriteRule ^(.*)/(.*) $1/${hostmap:%{HTTP_HOST}}/$2 [L]


Try this rule:

RewriteCond %{HTTP_HOST} ^(skin\d+)\.example\.com$
RewriteCond %{DOCUMENT_ROOT}$1/%1/$2 -f
RewriteRule ^([^/]+)/([^/]+)$ $1/%1/$2 [L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜