开发者

mod_rewrite where the rewrite rule matches a directory

I have a mod_rewrite rule like this:

RewriteRule ^(alice)/?$ bob.php?page=$1 [L]

If I go to http://localhost/alice/, it reads bob.php (as expected). If I go to http://localhost/alice (no end slash), it reads bob.php (again, as expected).

If I create an directory in htdocs called "alice", and go to http://localhost/alice/, it reads bob.php. But, if I go to http://localhost/alice, it now redirects to http://localhost/alice/?page=alice. It still reads bob.php (which is what I want), but I'd rather 开发者_JAVA百科it wasn't altering the URL like that.

Why is it doing that, and is there any way of preventing it (other than just not having such directories lying about)?


RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(alice)/?$ bob.php?page=$1 [L]


It seems to be caused by the /? at the end - removing the ? causes (when the alice directory exists):

  • /alice to be redirected to /alice/, and displayed as /bob.php?page=alice
  • /alice/ to display as /bob.php?page=alice

If the alice directory does not exist, /alice will not be redirected (but /alice/ will be). If you know whether the directory exists or not, you can choose whether to use / or /?.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜