簡體   English   中英

如何使用.htaccess將所有http流量重定向到https和www.domain.com

[英]how to use .htaccess to redirect all http traffic to https AND www.domain.com

我已經嘗試過這里發布的幾種解決方案,但它們似乎都不起作用-我只是遇到重定向錯誤。

我想做的是將所有流量重定向到https:

http://domain.com
http://www.domain.com
https://domain.com
http://alias.com
http://www.alias.com
https://alias.com

應該都去:https://www.domain.com

當前地址開頭為:http:// www將不會重定向到https。

這是htaccess文件

RewriteEngine On
RewriteBase /

# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L] 

# Exclude /assets and /manager directories from rewrite rules
RewriteRule ^(manager|assets) - [L]

# For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^myalias.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www.myalias.ca$
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L]

RewriteCond %{HTTP_HOST} ^myotheralias.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www.myotheralias.ca$
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L]

您應該擺脫底部的www重定向:

RewriteCond %{HTTP_HOST} ^myalias.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www.myalias.ca$
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L]

RewriteCond %{HTTP_HOST} ^myotheralias.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www.myotheralias.ca$
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L]

不需要它們,您只需要在頂部進行單個重定向,然后替換:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L] 

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]

這應該照顧所有重定向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM