apache 如何将 HTTPS 重定向到 HTTP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:41:34  来源:igfitidea点击:

How do you redirect HTTPS to HTTP?

apachesslredirecthttps

提问by mauriciopastrana

How do you redirect HTTPS to HTTP?. That is, the opposite of what (seemingly) everyone teaches.

如何将 HTTPS 重定向到 HTTP?也就是说,与(表面上)每个人所教的相反。

I have a server on HTTPS for which I paid an SSL certification for and a mirror for which I haven't and keep around for just for emergencies so it doesn't merit getting a certification for.

我在 HTTPS 上有一个服务器,我为它支付了 SSL 认证,还有一个镜像,我没有,只是为了紧急情况而保留,所以它不值得获得认证。

On my client's desktops I have SOME shortcuts which point to http://production_serverand https://production_server(both work). However, I know that if my production server goes down, then DNS forwarding kicks in and those clients which have "https" on their shortcut will be staring at https://mirror_server(which doesn't work) and a big fat Internet Explorer 7 red screen of uneasyness for my company.

在我客户的桌面上,我有一些指向http://production_serverhttps://production_server(都有效)的快捷方式。但是,我知道如果我的生产服务器出现故障,那么 DNS 转发就会启动,那些在快捷方式上带有“https”的客户端将盯着https://mirror_server(这不起作用)和一个巨大的 Internet Explorer 7 红屏不安为我的公司。

Unfortunately, I can't just switch this around at the client level. These users are very computer illiterate: and are very likely to freak out from seeing HTTPS "insecurity" errors (especially the way Firefox 3 and Internet Explorer 7 handle it nowadays: FULL STOP, kind of thankfully, but not helping me here LOL).

不幸的是,我不能只是在客户端级别进行切换。这些用户对计算机非常了解:并且很可能会因为看到 HTTPS“不安全”错误而惊慌失措(尤其是现在 Firefox 3 和 Internet Explorer 7 处理它的方式:FULL STOP,有点谢天谢地,但在这里对我没有帮助,哈哈)。

It's very easyto findApache solutionsfor http->https redirection, but for the life of me I can't do the opposite.

这是很容易找到的Apache的解决方案基于HTTP> HTTPS重定向,但对我的生活,我不能反其道而行之。

Ideas?

想法?

采纳答案by ejunker

This has not been tested but I think this should work using mod_rewrite

这尚未经过测试,但我认为这应该可以使用 mod_rewrite

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

回答by Kieron

Keep in mind that the Rewrite engine only kicks in once the HTTP request has been received - which means you would still need a certificate, in order for the client to set up the connection to send the request over!

请记住,重写引擎仅在收到 HTTP 请求后才会启动 - 这意味着您仍然需要证书,以便客户端建立连接以发送请求!

However if the backup machine will appear to have the same hostname (as far as the client is concerned), then there should be no reason you can't use the same certificate as the main production machine.

但是,如果备份机器看起来具有相同的主机名(就客户端而言),那么您应该没有理由不能使用与主生产机器相同的证书。

回答by antoniom

Based on ejunker's answer, this is the solution working for me, not on a single server but on a cloudenviroment

基于 ejunker 的回答,这是对我有用的解决方案,不是在单个服务器上,而是在环境中

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

回答by Rick

For those that are using a .conffile.

对于那些使用.conf文件的人。

<VirtualHost *:443>
    ServerName domain.com
    RewriteEngine On
    RewriteCond %{HTTPS} on
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/domain.crt
    SSLCertificateKeyFile /etc/apache2/ssl/domain.key
    SSLCACertificateFile /etc/apache2/ssl/domain.crt

</VirtualHost>

回答by rg88

If none of the above solutions work for you (they did not for me) here is what worked on my server:

如果上述解决方案都不适用于您(它们不适用于我),那么这在我的服务器上有效:

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ [L,R=301]

回答by mikulabc

all the above did not work when i used cloudflare, this one worked for me:

当我使用 cloudflare 时,上述所有内容都不起作用,这个对我有用:

RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and this one definitely works without proxies in the way:

这个绝对可以在没有代理的情况下工作:

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

回答by sys0dm1n

It is better to avoid using mod_rewrite when you can.

如果可以,最好避免使用 mod_rewrite。

In your case I would replace the Rewrite with this:

在你的情况下,我会用这个替换重写:

    <If "%{HTTPS} == 'on'" >
            Redirect permanent / http://production_server/
    </If>

The <If>directive is only available in Apache 2.4+ as per this blog here.

根据此处的博客,<If>指令仅在 Apache 2.4+ 中可用。

回答by jaxarroyo

this works for me.

这对我有用。

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
    Redirect "https://www.example.com/" "http://www.example.com/"
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    # ... 
</VirtualHost>

be sure to listen to both ports 80 and 443.

确保同时监听 80 和 443 端口。

回答by Yuseferi

None of the answer works for me on Wordpress website but following works ( it's similar to other answers but have a little change)

在 Wordpress 网站上,没有一个答案对我有用,但可以参考(它与其他答案类似,但有一点变化)

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

回答by RobinUS2

As far as I'm aware of a simple meta refresh also works without causing errors:

据我所知,一个简单的元刷新也不会导致错误:

<meta http-equiv="refresh" content="0;URL='http://www.yourdomain.com/path'">