Apache .htaccess行事奇怪吗?

时间:2015-10-04 06:54:05

标签: .htaccess mod-rewrite

我的.htaccess文件如下所示:

#Options FollowSymLinks

RewriteEngine On

RewriteCond   %{QUERY_STRING}   ^(.*)$
RewriteRule   ^([a-zA-Z0-9\-\/]*[a-zA-Z0-9])$   index.php?_path=$1&%1

这是我的文件结构:

example.com/
 |- rewriteTest/
 |   |- images/
 |   |   |- logo.png
 |   |   |- icon.png
 |   |- style/
 |   |   |- housekeeping.css
 |   |   |- style.css
 |   |- .htaccess
 |   |- index.php

理想的行为是

example.com/rewriteTest/any/path

应该指向

example.com/rewriteTest/index.php?_path=any/path

example.com/rewriteTest/images

(没有前导斜杠)应该指向

example.com/rewriteTest/index.php?_path=images

example.com/rewriteTest/images/logo.png

应该指向

example.com/rewriteTest/index.php?_path=images/logo.png

如果路径不是现有目录或文件,则重写有效。但是,如果我导航到/images目录,则我的浏览器(Chrome)中的网址会更改为/images/?_path=images并显示403页。如果我导航到/images/logo.png,图片会打开,而不是在/index.php?_path=images/logo.png显示页面。

为什么会发生这种情况?如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的正则表达式似乎是一个问题。试试这个:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /rewriteTest/

RewriteRule ^index\.php$ - [L,NC]

RewriteCond %{QUERY_STRING} !(^|&)_path=[^&]+ [NC]
RewriteRule ^(.+)$ index.php?_path=$1 [L,QSA]

另外,要打开目录网址,请使用http://domain.com/images/

之类的尾部斜杠