2010-11-08 62 views
1

我想刪除PHP擴展與htaccess,但也拒絕從所有的直接訪問PHP文件。所以,如果你寫www.site.com/test,你ok和www.site.com/test.php你無法訪問! Thx刪除php擴展但拒絕直接訪問

回答

1

我知道這是一個古老的問題,但我想做同樣的事情,並使用zerkms的答案清理,但修改,因此它不會中斷索引,並且不會在循環時打破循環對於不存在的文件。這裏的代碼是可行的,使用「error.php」作爲我的錯誤頁面而不是403.html。

RewriteEngine on 

#specify the error page 
ErrorDocument 404 /error.php?e=404 
ErrorDocument 403 /error.php?e=403 
ErrorDocument 401 /error.php?e=401 

#prevent redirect loops 
RewriteCond %{ENV:REDIRECT_STATUS} !^$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .* /error.php [L] 

#redirect to .php files but prevent direct access 
RewriteRule ^$ index.php [L,QSA] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*)/$ $1/index.php [L,QSA] 
RewriteCond %{THE_REQUEST} \.php 
RewriteCond %{REQUEST_FILENAME} !error.php$ 
RewriteRule .* /error.php [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !\.php$ 
RewriteRule (.*) $1.php [L,QSA]