2017-06-20 96 views
1

這裏是我隱藏的.html和.php擴展名,當用戶查看我的網站時,http://example.com/xxx.htmlhttp://example.com/xxx 但是我希望當用戶輸入http://example.com/xxx.html時,他們不能訪問並跳轉到404找不到。如何使用.htaccess來隱藏擴展名並阻止用戶使用擴展名URL進行訪問?

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks 
    RewriteEngine On 
    RewriteBase/

# Add trailing slash to url 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$ 
    RewriteRule ^(.*)$ $1/ [R=301,L] 

# Remove .php-extension from url 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^([^\.]+)/$ $1.php 

# Remove .html-extension from url 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^([^\.]+)/$ $1.html 

# End of Apache Rewrite Rules 
</IfModule> 

任何人都有解決方案嗎?

回答

0

您可以將其添加爲您的第一條規則:

RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC] 
RewriteRule^- [R=404,L] 

更新

也許這將解決您的問題ErrorDocument

RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC] 
RewriteCond %{REQUEST_URI} !=/error404.html 
RewriteRule^- [R=404,L] 
+0

似乎確定,但我用的.htaccess將錯誤404更改爲/error404.html,就像這樣'ErrorDocument 404/error404.html'如你所說,RewriteRule將重定向到404找不到。但在這種情況下,我輸入'http:// example.com/xxx.html'將不會重定向到/error404.html。它顯示另一個404頁面(可能是默認的一個,但實際上與默認的頁面不同)。它說:「在此服務器上找不到請求的URL /index.html。」 此外,嘗試使用ErrorDocument處理請求時遇到404 Not Found錯誤。 – Samson

+0

我添加了一個更新,因爲它似乎也許404錯誤是從相同的規則得到404。 – SuperDuperApps

+0

謝謝,它的工作!另一個問題,如何防止用戶訪問例如'http:// example.com/index /'導致它會使圖像和資產鏈接不​​起作用。希望通過重寫來阻止'index'這個詞,並重定向到/error404.html。特別感謝! – Samson

相關問題