2015-10-04 47 views
1

我的.htaccess文件看起來是這樣的:Apache .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 

重寫作品,如果路徑I不是現有的目錄或文件。但是,如果我導航至/images目錄,則瀏覽器中的網址(Chrome)會更改爲/images/?_path=images,並顯示403頁。如果我導航到/images/logo.png,圖像打開,而不是在/index.php?_path=images/logo.png顯示頁面。

爲什麼會發生這種情況,我該如何解決這個問題?

回答

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] 

另外打開一個目錄URL使用斜線狀http://domain.com/images/

+0

這是行不通的。現在我得到了任何URL的403 Forbidden。我不確定正則表達式是否是問題,因爲它適用於其他任何事情。我的問題是,當我導航到實際存在的路徑時,似乎mod_rewrite會中斷。 – hugabor

+0

取消註釋'選項FollowSymLinks'使我的index.php由於某種原因變爲禁止。 – hugabor

+0

'.htaccess'位於圖像的父目錄中,並與'index.php'一起。 – hugabor