我一直在尋找完美的301重定向。但我找到了很多解決方案,不知道什麼是最好的。最佳實踐:301將HTTP重定向到HTTPS(標準域)
這是我想要做的
- http://domain.tld/→https://domain.tld/
- http://www.domain.tld/→https://domain.tld/
- https://www.domain.tld/→https://domain.tld/
最佳實踐.htacess?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule^https://%1%{REQUEST_URI} [R=301,L]
</IfModule>
這是我的首選代碼。至少現在開始。
替代方式
我也發現了很多其他的方法來重定向從HTTP
到HTTPS
。例如:
1.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
缺少一個步驟?並且在這裏沒有[R=301,L]
?
2.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Is a different order generally better?
我應該使用的
RewriteRule ^(.*)$
代替
RewriteRule (.*)
?
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} example\.com$ [NC]
RewriteRule^https://example.com%{REQUEST_URI} [R=301,L,NE]
是否使用完整的域名有什麼性能優勢?我真的需要NE
嗎? ([R=301,L,NE]
與[L,R=301]
)
所以,我的問題都是專家:什麼是最好的(表演)的方式無論從HTTP
重定向到HTTPS
和到
HTTPS://
?
非常感謝。我現在使用這個代碼: RewriteEngine On RewriteCond%{HTTPS} off RewriteRule ^(。*)$ https://example.com/%{REQUEST_URI} [R = 301,L ,NE] RewriteCond%{HTTP_HOST}^www \。(。+)$ [NC] RewriteRule^https://%1%{REQUEST_URI} [R = 301,L,NE] –
dash
2015-03-13 19:27:15
我知道它是一個稍微老的答案,但不會從替代方案3的返回中返回,例如'RewriteRule^https://domain.tld/% {REQUEST_URI} [R = 301,L,NE]',例如https://domain.tld//index.html用雙斜槓?不應該在靜態域文本和'%{REQUEST_URI}'之間沒有斜線的情況下讀取'RewriteRule^https://domain.tld/% {REQUEST_URI} [R = 301,L,NE]'? – 2017-11-07 08:32:03
你是對的:我剛纔更正了這個 – nlu 2017-11-09 14:06:00