开发者

Rewrite only if Folder exists but file doesn't

I am trying to rewrite all requests to a file that doesn't exist but the folder does.

What I mean is:

Say I have 开发者_JAVA百科this folder structure:

foo
  '-bar1
  '-bar2
    '-bar2.html
  '-shared
     '-shared.html

What I am looking for the rewrite rule to do is to serve up example.com/foo/bar2/bar2.html as normal. Serve example.com/foo/bar1/bar1.html as /foo/shared/shared.html and to no serve example.com/foo/bar3/bar3.html.

So in summary. I am trying to develop a RewriteCond that hits only when the directory exists but the file doesnt exist in that directory.

The problem with:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

is that it will rewrite www.example.com/bar3/bar3.html even though /foo/bar3 directory doesn't exist.

Thanks for the help!


These rules should do the job:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/ -d
RewriteRule ^(.+)/([^/]+)$ /foo/shared/shared.html [L,QSA]
  1. These rules intended to be placed in .htaccess file in root folder. If you need to place them in config file (e.g. httpd-vhost.conf) then you will need to modify it a bit (remove some slashes in 2nd RewriteCond .. or add leading slash in RewriteRule -- testing is actually required).

  2. These rules will only work if requested resource has at least 1 folder (obvious -- as requested).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜