1
這裏是我隱藏的.html和.php擴展名,當用戶查看我的網站時,http://example.com/xxx.html
到http://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>
任何人都有解決方案嗎?
似乎確定,但我用的.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
我添加了一個更新,因爲它似乎也許404錯誤是從相同的規則得到404。 – SuperDuperApps
謝謝,它的工作!另一個問題,如何防止用戶訪問例如'http:// example.com/index /'導致它會使圖像和資產鏈接不起作用。希望通過重寫來阻止'index'這個詞,並重定向到/error404.html。特別感謝! – Samson