2013-12-19 148 views
1

我寫了這段代碼將所有內容重定向到index.php。拒絕直接訪問php文件

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [QSA] 

我想以某種方式調整!-f允許.php文件被301重定向到/沒有進入重定向循環。我有/foo/bar/218加載index.php這需要view/foo_bar.php。我想通過地址欄無法訪問/view/foo_bar.php

回答

1

您可以嘗試以下規則:

# redirect .php requests to/
RewriteCond %{THE_REQUEST} \.php[\s?] [NC] 
RewriteRule ^/[L,R=302] 

# existing rules without .php files 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule !\.php$ index.php [QSA] 
+0

謝謝你,它的工作原理。你能告訴我,這部分是什麼?[\'s?]'? –

+1

確定'[\ s?]'在正則表達式中被稱爲字符類,它與'.php'後面的空格或立即數'?'匹配。 – anubhava