使用。htaccess HTTP和HTTPS之间正确地切换使用。htaccess HTTP和HTTP

2019-06-02 11:10发布

我们有我们在共享主机(MEDIATEMPLE Gridserver)上举办其购物网站。 该网站的某些部分需要使用HTTPS(结帐等),但剩下的应该是使用HTTP。

有谁知道我们如何能始终强制为特定的URL正确使用HTTP / HTTPS的? 我们已经受够了在各种状态下工作,但我们不能得到一个网页,应该是HTTP,但要求与HTTPS正确切换回的请求。

我有一个走一走,看一看,但没找到合适的答案。

Answer 1:

我用类似这样的东西在WordPress我的管理文件夹:

#redirect all https traffic to http, unless it is pointed at /checkout
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/checkout/?.*$
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

RewriteCond %{HTTPS} on部分可能不适用于所有的Web服务器上运行。 我的虚拟主机提供商需要RewriteCond %{HTTP:X-Forwarded-SSL} on ,例如。

如果要强制相反,尝试:

#redirect all http traffic to https, if it is pointed at /checkout
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/checkout/?.*$
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

如果你想一些替代的方法来做到这一点,看看askapache 。



Answer 2:

这应该工作在几乎所有情况,并应在实际的虚拟主机或工作的.htaccess:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}/%{REQUEST_URI} [R=301,L]

(不要忘记%{REQUEST_URI}之前斜线,因为这可以允许传递一个端口号,这是危险的)



Answer 3:

RewriteEngine on
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{SERVER_NAME}/%{REQUEST_URI} [R=301,L]

我有一些问题是一个loadballancer后面。 这怎么我固定它。



Answer 4:

正如详细这个答案 ,解决您的应用程序使用https://需要时联系。 不要依赖于自动重定向,如果你还没有你的链接,这可能导致你的安全的错觉/表格服务过https://https://的网址了。 使用mod_rewrite自动使得它更难检测这样的错误(其也可以是漏洞)。



Answer 5:

对我来说,这个工作(我用了WordPress站点和重定向到HTTPS)。 你要添加的条件和基准线仅落后RewriteEngine叙述和RewriteBase线:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# I added these two lines for redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]
# (end of custom modifications)

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress`

看看调理RewriteCond %{HTTP:X-Forwarded-Proto} !https -只有这个工作对我的服务器托管。 (我试过RewriteCond %{SERVER_PORT} !^443$RewriteCond %{HTTPS} off为好,但没有成功。



Answer 6:

我觉得应该是:

RewriteCond %{HTTPS}  =on
^/checkout(.*) http://shoppingsite.com/checkout$1 [R]

见mod_rewrite的文档。



文章来源: Correctly switching between HTTP and HTTPS using .htaccess