2012-05-08 35 views
1

我在這裏問了一個[question]:htaccess reverse directory關於反向路由。但是,我不斷收到目錄視圖,而不是有問題的文件。Htaccess權限被拒絕/目錄索引顯示

例如:我去/img/header.jpg當文件header.jpg存在時,我得到文件夾/ img /的內容。我在選項中添加了-Indexes,但這隻會導致403禁止訪問消息。

我該如何編輯我的htaccess來顯示imgs/js/css等,但仍然保持遞歸結構?

當前htacces:提前

Options +FollowSymLinks -MultiViews -Indexes 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f 
RewriteRule ^(.*?)/([^/]+)/?$ $1/ [L] 

RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 

感謝

編輯

我都試過後直接RewriteBase /添加以下行:

RewriteCond %{REQUEST_FILENAME} !-f 

這項工作適用於大多數文件。只有當它是圖像/ css/js/ico /等時,才能使用它。我認爲現在如果它直接找到一個PHP文件,它會工作。

我似乎弄不清楚是如何獲得其餘參數。

/index/foo/bar/for/ 

應該找到的文件是foo,如何獲得$ _GET中剩餘的2個參數?

回答

0

你的代碼更改爲:

選項+的FollowSymLinks -MultiViews -Indexes

# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# If the request is for a valid file 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
# If the request is for a valid link 
RewriteCond %{REQUEST_FILENAME} -l 
# don't do anything 
RewriteRule^- [L] 

# if current ${REQUEST_URI}.php is not a file then 
# forward to the parent directory of crrent REQUEST_URI 
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f 
RewriteRule ^(.*?)/([^/]+)/?$ $1/ [L] 

# if current ${REQUEST_URI}.php is a valid file then 
# load it be removing optional trailing slash 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 

編輯:基於OP的評論這個解決方案填補了一個查詢參數param與路徑的其餘部分:

# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# If the request is for a valid file 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
# If the request is for a valid link 
RewriteCond %{REQUEST_FILENAME} -l 
# don't do anything 
RewriteRule^- [L] 

# if current ${REQUEST_URI}.php is not a file then 
# forward to the parent directory of crrent REQUEST_URI 
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f 
RewriteCond %{QUERY_STRING} ^(?:param=)?(.*)$ 
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param=$2/%1 [L] 

# if current ${REQUEST_URI}.php is a valid file then 
# load it be removing optional trailing slash 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 
+0

很酷,這似乎工作。我如何將剩下的參數作爲$ _GET ['params']?我假設第三個陳述檢查時,參數丟失了嗎? – John

+0

我不確定'$ _GET ['params']'是否基本上是查詢參數。正如你可以看到這些規則不處理任何查詢字符串。如果你能告訴我你的原始URI是什麼以及你期望什麼樣的行爲會更好? – anubhava

+0

我期待:/ some/place/foo/bar去/some/place.php獲取變量$ _GET ['param] ='foo/bar'; – John