开发者

Rewrite Rule with dynamic hostname

I'm interesting in rewrite rule to modify some url and paths. Here some Examples. I have some hostnames which lookups to 1 VirtualHost throught ServerAlias in Apache conf.

example.com

anotherdomain.com

same.org

And a lot of anothers domains. Here, what I want to do:

example.com/uploads/photo.png -> /www/example.com/uploads/example.com/photo.png<br />

anotherdomain.com/uploads/photo.png -> /www/anotherdomain.com/uploads/anotherdomain.com/photo.png<br />

And same thing with another domains. This thing I've made already.

RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^uploads/([a-zA-Z0-9-\._]+)$ uploads/%2/$1 [L]

This work perfectly. But, if I try to access uploads/icons/category/image.png

I start to see 500 Internal Apache Error... I've modify Rule to this:

RewriteCond %{REQUEST_URI} !^/uploads/%{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^uploads/(开发者_开发百科[a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]

And Enable Apache Rewrite log with 9 verbosity.

RewriteCond: input='/uploads/icons/category/image.png' pattern='!^/uploads/%{HTTP_HOST}' => matched
rewrite 'uploads/icons/category/image.png' -> 'uploads/example.com/icons/category/image.png'
RewriteCond: input='/uploads/example.com/icons/category/image.png' pattern='!^/uploads/%{HTTP_HOST}' => matched
rewrite 'uploads/example.com/icons/category/image.png' -> 'uploads/example.com/example.com/icons/category/image.png'

And after that, it's exhausts 10 lookups =(

When I modify to this

RewriteCond %{REQUEST_URI} !^/uploads/example.com
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^uploads/([a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]

It's works perfectle for ALL stuff, but only for 1 domain: example.com

I found another solution, but it's not a perfect.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^uploads/([a-zA-Z0-9-\._/]+)$ uploads/%2/$1 [L]

This Rule works only if I have existed dir and file. But, if i try to access:

/uploads/images/not-existed-404.nothing -> It return 500, because this file doesn't exists and rewriting into:

/uploads/example.com/example.com/example.com/example.com.......

P.S. Sorry for my bad English ;)


RewriteCond %{REQUEST_URI} !^/uploads/%{HTTP_HOST}

You CANNOT use variables or back references in pattern -- i.e. %{HTTP_HOST} will not be expanded to example.com, instead it will be treated as normal text.


You can uses these rules (working fine on my PC):

RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/uploads/%2/$1 -f
RewriteRule ^uploads/(.+)$ uploads/%2/$1 [L]
  1. This rule will check if target file does exist and only then rewrites (order of conditions matter).

  2. Let's assume:

    • domain name: example.com
    • website root: /www/example.com/
    • URL requested: /uploads/img/meow.png

    Second condition (RewriteCond %{DOCUMENT_ROOT}/uploads/%2/$1 -f) will check if file /www/example.com/uploads/example.com/img/meow.png exists, and if it does -- then rewrite occurs.

Your .htaccess then may look like this:

RewriteEngine On
RewriteBase /

# do not do anything for already existing files & folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# rewrite uploads to domain-specific folder
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/uploads/%2/$1 -f
RewriteRule ^uploads/(.+)$ uploads/%2/$1 [L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜