我想用https://example.com 上的.htaccess 檔案做兩件事
我希望所有檔案路徑都不區分大小寫。目前我所有的檔案和檔案夾都以大寫字母開頭。
我希望能夠在不添加檔案擴展名的情況下查看和訪問所有檔案。
我當前的 .htaccess 檔案:
ErrorDocument 401 https://www.example.com
ErrorDocument 403 https://www.example.com
ErrorDocument 404 https://www.example.com
ErrorDocument 500 https://www.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTP_HOST} ^example\.com\.subdomains\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
uj5u.com熱心網友回復:
- 我希望所有檔案路徑都不區分大小寫。目前我所有的檔案和檔案夾都以大寫字母開頭。
由于底層檔案系統路徑是大小寫混合的,您將需要使用 mod_speling (one l)。(您需要訪問服務器配置以確保啟用此模塊。)
然后您可以在以下內容中使用.htaccess:
# Allow mixed case requests (mod_speling)
CheckSpelling on
CheckCaseOnly on
但是請注意,這會觸發到正確大小寫的 URL 的外部重定向。因此,如果用戶請求/foo/bar/baz,他們將被外部重定向到/Foo/Bar/Baz.
但是,如果這會生成 500 內部服務器錯誤,則可能未安裝 mod_speling。
- 我希望能夠在不添加檔案擴展名的情況下查看和訪問所有檔案。
我假設您指的只是作為頁面請求一部分的主.html(或.php)檔案,而不是所有檔案,例如所有靜態資源,例如。image/ image.jpg, mystyles/mystyles.css等
您需要在 HTTP 到 HTTPS 重定向之后和將不存在的請求發送到外部服務器的重定向之前立即添加如下內容。
# Rewrite request to append file extension. eg. "/foo" to "/foo.html"
RewriteCond %{REQUEST_URI} !(\.\w{2,4}|/)$
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (. ) $1.html [L]
第一個條件避免檢查請求尚未以檔案擴展名(或斜杠 - 表示目錄)結束。這樣做的副作用是在最終路徑段中不能有“看起來像”檔案擴展名的點(通常可以避免的邊緣情況)。例如。/example.foo不會被重寫/example.foo.html(如果該檔案應該存在)。但/foo.example可能會被重寫。這是一種優化,可以避免大量不必要的檔案系統檢查。
第二個條件.html在重寫請求之前檢查相應的存在。
您需要確保始終鏈接到沒有檔案擴展名的 URL,以避免潛在的重復內容問題(并將檔案擴展名暴露給您的用戶)。
如果您要更改現有 URL 結構,則可以實施外部重定向以.html從直接請求的 URL 中洗掉檔案擴展名。這對于保留 SEO 和任何反向鏈接是必要的。但是,這不能替代更改 HTML 源代碼中的內部鏈接以洗掉.html擴展名。
如果需要,則在上述“重寫”之前添加以下內容:
# Redirect - remove the ".html" extension, eg. "/foo.html" to "/foo"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (. )\.html$ /$1 [R=301,L]
注意:首先使用 302(臨時)重定向進行測驗以避免潛在的快取問題。
更新#1:
它使用 LiteSpeed 服務器。
不幸的是,LiteSpeed 目前不支持 mod_speling - 這是一個僅限 Apache 的模塊。
And (unfortunately)... LiteSpeed does not "break" when it reads an error in the .htaccess file - it simply ignores it (silently). Which makes debugging these things that much harder.
You won't be able to make the URLs case-insensitive on LiteSpeed without a lot of work (read: it's not worth it). Just make sure you use correctly cased URLs throughout your site (or convert everything to lowercase).
UPDATE#2:
I'm wanting that to work for all files including images and other static files.
Unless you have a very good reason for doing this I would recommend against that. Again, LiteSpeed does not support MultiViews/mod_negotiation - which would be "the easy" way to do this on Apache. (However, MultiViews can conflict with rewrites you might add later and cause you other problems unless you are experienced with this.)
To do this on LiteSpeed would be a rather tedious (and resource-intensive) process of manually checking every file extension to see whether it exists.
You can also have mime-type issues since the browser is unable to infer anything from the file extension. And users that downloaded anything will have to manually add the appropriate file extension for it to "work" locally.
This also opens you up to many "duplicate" issues unless you implement the redirect to strip the file extension.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337150.html
標籤:.htaccess
上一篇:了解舊的.htaccess檔案
