我想在整個站點中強制使用 HTTPS,這已經完成了以下操作沒有問題:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|ico|js|css|woff|ttf|eot|svg)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC,NE]
但我也想確保去頂點域的請求被重定向到www.example.com
我找到了這個答案,但似乎這也會將子域重定向到www嗎?...或者也許它不會因為子域不使用自己的.htaccess?
除了上述之外,我還嘗試了以下www.部分,但它似乎不起作用?
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|ico|js|css|woff|ttf|eot|svg)|webp$ [NC]
RewriteRule ^ https://www.example.com/%{REQUEST_URI} [R=301,L,NC,NE]
我認為如果它不以它開頭就會失敗example.com,因此會留下任何子域,包括www?
但它似乎根本沒有觸發。
uj5u.com熱心網友回復:
此配置應滿足您的需求:
RewriteEngine On
# When on the root domain (with HTTPs; on "www" or the root domain only):
# Redirect to WWW Root
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(?!www\.)(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NC,NE]
# When HTTP is off (on "www" or the root domain only)
# Redirect to the HTTPs Version of the WWW root
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.|)(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NC,NE]
您還可以使用此網站測驗您的配置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/415613.html
標籤:
