我有一個網站,其子頁面的 URL 如下所示:
https://www.example.com/hello/world
https://www.example.com/hello/earth
在 cpanel 中,這些頁面的檔案結構如下所示:
[folder]
hello
└ [folder]
world.html
earth.html
.htaccess
index.html
我的 .htaccess 檔案具有以下規則來說明缺少 .html 檔案擴展名:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.] )$ $1.html [NC,L]
如何將https://www.example.com/hello負載作為作業網頁?
我試過簡單地將 hello.html 添加到檔案夾中,但無濟于事——就像這樣:
[folder]
hello
└ [folder]
world.html
earth.html
.htaccess
hello.html
index.html
當我嘗試https://www.example.com/hello在該示例中訪問時,它會將我帶到我的 404 頁面,但.../hello/world仍然.../hello/earth可以作業。https://www.example.com/hello.html確實有效,但我不希望 .html 檔案擴展名對最終用戶可見。
在這種情況下,我還沒有找到隱藏 .html 檔案擴展名的解決方案。幫助這樣做將不勝感激!
uj5u.com熱心網友回復:
當我嘗試
https://www.example.com/hello在該示例中訪問時,它會將我帶到我的 404 頁面
因為您有一個同名目錄,并且(默認情況下)mod_dir 將發出 301 重定向以修復/附加尾部斜杠。然后,當您的指令嘗試.html通過將請求(現在帶有尾部斜杠)從/hello/to /hello/.html(這自然導致 404)重寫來附加檔案擴展名時,重定向請求會產生 404。
為了防止 mod_dir 在目錄請求中附加斜杠,您可以在檔案頂部包含以下指令:
# Prevent mod_dir appending a trailing to directory requests
DirectorySlash Off
# Disable auto-generated directory listings (mod_autoindex)
Options -Indexes
您需要確保在測驗之前清除瀏覽器快取,因為較早的 301(永久)重定向(通過 mod_dir)將被瀏覽器永久快取。
為了安全起見,您還需要確保禁用自動生成的目錄串列(除非您明確需要此行為),因為DirectoryIndex Off設定了 when 并且您請求不帶斜杠的目錄,mod_autoindex 仍會生成目錄串列,即使Directoryindex檔案存在于該目錄中。
替代解決方案
或者,您仍然可以允許 mod_dir 附加尾部斜杠(因此對的請求/hello仍然被重定向到規范 URL/hello/也是如此/hello/),但允許在RewriteRule 模式中使用可選的尾部斜杠,但通過使用將其從捕獲子模式中排除非貪婪的正則運算式。
例如:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.] ?)/?$ $1.html [L]
帶有括號的子模式會捕獲對/hello/then的請求hello,因此將其重寫hello.html為如上所述。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/449229.html
上一篇:通過.htaccess重定向Wordpress構建的站點,同時排除根url、wp-admin目錄和wp-login.php
