我subdomain.example.com重定向到www.subdomain.example.com.
訪問代碼:
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
我在尋找
example.com到www.example.com
anything.example.com到anything.example.com
我怎樣才能解決這個問題?
更新:我為子域和根域使用一個 htaccess 檔案,出于某種原因,我不能為不同的域使用單獨的 htaccess 檔案。
注意:請不要將此問題標記為重復。我已經搜索了 stackoverflow 的其他部分,但沒有找到可行的解決方案。
uj5u.com熱心網友回復:
您需要限制重寫條件以僅匹配頂級域:
# ensure www. to top level domain only
RewriteCond %{HTTP_HOST} ^[\w-] \.(com|net|in|co\.uk)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
確保在測驗前清除瀏覽器快取。
uj5u.com熱心網友回復:
解決方案:https : //stackoverflow.com/a/35285128/4643961
### START WWW & HTTPS
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^.] \.[^.] \.[^.] )$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
選擇 :
### START WWW & HTTPS
# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.] \.[^.] $
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^.] \.[^.] \.[^.] )$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
uj5u.com熱心網友回復:
供將來參考:
# ensure www. to top level domain only
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/405634.html
標籤:
