2011-10-01 68 views
4

我正在尋找一種方法來通過.htaccess隱藏文件擴展名並拒絕直接訪問。讓我們考慮以下幾點:重寫文件擴展名但是拒絕直接訪問文件

http://www.xyz.zyx/index.php 

被轉換爲

http://www.xyz.zyx/index OR http://www.xyz.zyx/ 

都好到現在。接下來我想做的事情是在用戶嘗試直接訪問時阻止或重定向。例如,如果該URL的用戶類型欄中鍵入以下(延伸),塊或重定向:

http://www.xyz.zyx/index.php 

我檢查來自其他問題其他的答案,但不似乎是準確的。

在此先感謝您幫助像我這樣的新手。

+0

的可能重複[重定向* .PHP清潔URL]( http://stackoverflow.com/q/2267488/),[隱藏擴展名.php在url mod_rewrite](http://stackoverflow.com/q/6241408/) – outis

回答

5

儘管有人可能會質疑這樣的事情的有效性,這是可行的,所以在這裏是如何使用mod_rewrite做在.htaccess

RewriteEngine On 

# Sets your index script 
RewriteRule ^$ index.php [L] 

# Condition prevents redirect loops (when script is not found) 
RewriteCond %{ENV:REDIRECT_STATUS} !^$ 
RewriteCond %{REQUEST_FILENAME} !-f 

# Stop here if the file is not found after a redirect 
RewriteRule ^(.*)$ notfound.php [L] 

# Condition prevents redirect loops (when script is found) 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 

# Forbid access directly to PHP files 
RewriteRule ^.*?\.php$ forbidden [F,L] 

# Make sure the filename does not actually exist (images, etc.) 
RewriteCond %{REQUEST_FILENAME} !-f 

# Append the .php extension to the URI 
RewriteRule ^(.*)$ $1.php [L] 
相關問題