2014-01-29 159 views
1

你好,我试图完成下面所有的index.php失败即使有一个与url相同的目录。假设我有一个名为'users'的文件夹,我不想让'http://local.dev/users'将文件重定向到该文件夹​​,而是像其他请求一样通过php来处理它。htaccess的重写时,具有相同名称的目录存在

3)我想能够访问任何类型的图像,js,css和其他特定文件类型 ,即使它们在目录中。

我到目前为止是这样的:

RewriteEngine on 
RewriteBase/
RewriteRule ^(.*)/$ $1 [R=301,L] 
RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|jpeg|bmp|gif|css|js|json|csv)$ [NC] 
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA] 

和PHP测试案例:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>+dev</title> 
    </head> 
    <body> 
     <?php 
      echo $_SERVER['REQUEST_URI'] . '<br>'; 
      print_r($_GET); 
      echo '<br>'; 
      include 'test/test.php'; 
     ?> 
     <img src="/image.jpg"> 
     <img src="/test/image2.jpg"> 
    </body> 
</html> 

不过,我注意到了以下问题:

请记住,我有一个在htaccess和index.php旁边的根目录中名为test的文件夹

http://local.dev/test/image.jpg =工作

http://local.dev/somestring =工作

http://local.dev/someImage.jpg =工作

http://local.dev/test/ =失败

http://local.dev/test = 2的情况下最后失败

浏览器的URL输出:

http://local.dev/test?path=test&path=test&path=test&path=test&path=test&path=test&path=test&path=test&path=test&path=test 

Yeap重定向循环,但我不明白为什么,因为我不熟悉mod_rewrite。我想要的是,最后2个失败的请求被视为纯文本请求,无论是否存在具有相同名称的文件夹。尽管我希望文件夹内的文件在扩展名位于“允许列表”中时可以访问。

回答

0

更改为:

RewriteEngine on 
RewriteBase/
RewriteRule ^(.*)/$ $1 [R=301,L] 
RewriteRule !\.(png|jpg|jpeg|bmp|gif|css|js|json|csv)$ index.php [NC,L] 
+0

我看到,虽然它的工作原理的图像加载失败这样 – Syd

+0

什么消息,你看到的图片?另外,考虑启用RewriteLog和RewriteLogLevel调试重写规则为图像和pastebin执行的操作,并在此处发布调试日志。请参阅:https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog – Rijndael

0

之所以发生这种情况是因为mod_dir and the DirectorySlash directive。从本质上讲,如果它看到一个没有结尾斜线的URI,并且它映射到一个现有的目录,那么它将重定向请求,使其具有尾部斜线。由于mod_dir和mod_rewrite都位于URL文件处理管道的不同位置,因此mod_dir和mod_rewrite都会应用到同一个URL。

所以,你需要打开DirectorySlash关闭(可以把你的htaccess文件这样的任何地方):

DirectorySlash Off 
+0

不行,不起作用 – Syd

相关问题