如何自动重定向所有不安全(http)的网页到他们的https对应http://example.com -> https://example.com,但只对支持https的浏览器这样做。所以我的网站仍然可以在较旧的浏览器中运行?
http://example.com -> https://example.com
xuo3flqw1#
此重定向代码可以直接添加到vhost文件中,也可以添加到.htaccess文件中:
RewriteEngine on# rewrite to https.# -----------------# %{HTTP:X-Forwarded-Proto} !https: This condition checks if the X-Forwarded-Proto header is not set to https. # The X-Forwarded-Proto #header is typically set by proxies or load balancers to indicate the original protocol # used for the request. By checking this header, # you can ensure that the redirect only occurs if the request # is not already using HTTPS.RewriteCond %{HTTP:X-Forwarded-Proto} !https# %{HTTP:Upgrade-Insecure-Requests} ^1$: This condition checks if the Upgrade-Insecure-Requests header is set to 1. # The Upgrade-Insecure-Requests header is sent by modern browsers that support HTTPS and can automatically upgrade # an insecure request to a secure one. By checking this header, you can verify if the browser supports HTTPS and # wants to upgrade the request to HTTPS.RewriteCond %{HTTP:Upgrade-Insecure-Requests} ^1$# RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteEngine on
# rewrite to https.
# -----------------
# %{HTTP:X-Forwarded-Proto} !https: This condition checks if the X-Forwarded-Proto header is not set to https.
# The X-Forwarded-Proto #header is typically set by proxies or load balancers to indicate the original protocol
# used for the request. By checking this header, # you can ensure that the redirect only occurs if the request
# is not already using HTTPS.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
# %{HTTP:Upgrade-Insecure-Requests} ^1$: This condition checks if the Upgrade-Insecure-Requests header is set to 1.
# The Upgrade-Insecure-Requests header is sent by modern browsers that support HTTPS and can automatically upgrade
# an insecure request to a secure one. By checking this header, you can verify if the browser supports HTTPS and
# wants to upgrade the request to HTTPS.
RewriteCond %{HTTP:Upgrade-Insecure-Requests} ^1$
# RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
在内部,它检查Upgrade-Insecure-Requests头,这是浏览器发送的内容。基于此请求标头,它重定向页面。
1条答案
按热度按时间xuo3flqw1#
此重定向代码可以直接添加到vhost文件中,也可以添加到.htaccess文件中:
在内部,它检查Upgrade-Insecure-Requests头,这是浏览器发送的内容。基于此请求标头,它重定向页面。