2014-01-29 68 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

相關問題