简体   繁体   English

使用htaccess Https到http重定向

[英]Https to http redirect using htaccess

I'm trying to redirect https://www.example.com to http://www.example.com . 我正在尝试将https://www.example.com重定向到http://www.example.com I tried the following code in the .htaccess file 我在.htaccess文件中尝试了以下代码

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This code successfully redirects https://example.com to http://www.example.com . 此代码成功将https://example.com重定向到http://www.example.com However when I type in https://www.example.com then it gives me a "web page not available" error in the browser. 但是,当我输入https://www.example.com时,它会在浏览器中显示“网页不可用”错误。

I have also tried the following 2 codes without success 我也尝试了以下2个代码但没有成功

Attempt 1 尝试1

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*):NOSSL$ http://www.example.com/$1 [R=301,L]

Attempt 2 尝试2

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

Both above attempts failed. 以上两种尝试均失败。 Any suggestions? 有什么建议?

Attempt 2 was close to perfect. 尝试2接近完美。 Just modify it slightly: 只需稍微修改一下:

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

However, if your website does not have a security certificate, it's on a shared hosting environment, and you don't want to get the "warning" when your website is being requested through https, you can't redirect it using htaccess. 但是,如果您的网站没有安全证书,则它位于共享托管环境中,并且您不希望在通过https请求您的网站时收到“警告”,您无法使用htaccess重定向它。 The reason is that the warning message gets triggered before the request even goes through to the htaccess file, so you have to fix it on the server. 原因是警告消息在请求甚至进入htaccess文件之前被触发,因此您必须在服务器上修复它。 Go to /etc/httpd/conf.d/ssl.conf and comment out the part about the virtual server 443. But the odds are that your hosting provider won't give you that much control. 转到/etc/httpd/conf.d/ssl.conf并注释掉有关虚拟服务器443的部分。但可能性是您的托管服务提供商不会给您那么多控制权。 So you would have to either move to a different host or buy the SSL just so the warning does not trigger before your htaccess has a chance to redirect. 因此,您必须移动到其他主机或购买SSL,以便在您的htaccess有机会重定向之前不会触发警告。

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

You can use the following rule to redirect from https to http : 您可以使用以下规则从https重定向到http

 RewriteEngine On


RewriteCond %{HTTPS} ^on$
RewriteRule ^(.*)$ http://example.com/$1 [NC,L,R]

Explanation : 说明:

RewriteCond %{HTTPS} ^on$

Checks if the HTTPS is on (Request is made using https) 检查HTTPS是否已启用 (使用https发出请求)

Then 然后

RewriteRule ^(.*)$ http://example.com/$1 [NC,L,R]

Redirect any request ( https://example.com/foo ) to http://example.com/foo . 将任何请求( https://example.com/foo )重定向 http://example.com/foo

  • $1 is part of the regex in RewriteRule pattern, it contains whatever value was captured in (.+) , in this case ,it captures the full request_uri everything after the domain name. $ 1是RewriteRule模式中的正则表达式的一部分,它包含在(。+)中捕获的任何值,在这种情况下,它捕获域名后的完整request_uri所有内容。

  • [NC,L,R] are the flags, NC makes the uri case senstive, you can use both uppercase or lowercase letters in the request. [NC,L,R]是标志,NC使uri情况敏感,您可以在请求中使用大写或小写字母。

L flag tells the server to stop proccessing other rules if the currunt rule has matched, it is important to use the L flag to avoid rule confliction when you have more then on rules in a block. 如果currunt规则匹配,L标志告诉服务器停止处理其他规则,当你在块中有更多的规则时,使用L标志来避免规则冲突是很重要的。

R flag is used to make an external redirection. R标志用于进行外部重定向。

The difference between http and https is that https requests are sent over an ssl-encrypted connection. http和https之间的区别在于https请求是通过ssl加密连接发送的。 The ssl-encrypted connection must be established between the browser and the server before the browser sends the http request. 在浏览器发送http请求之前,必须在浏览器和服务器之间建立ssl加密连接。

Https requests are in fact http requests that are sent over an ssl encrypted connection. Https请求实际上是通过ssl加密连接发送的http请求。 If the server rejects to establish an ssl encrypted connection then the browser will have no connection to send the request over. 如果服务器拒绝建立ssl加密连接,则浏览器将没有连接来发送请求。 The browser and the server will have no way of talking to each other. 浏览器和服务器无法相互通信。 The browser will not be able to send the url that it wants to access and the server will not be able to respond with a redirect to another url. 浏览器将无法发送它想要访问的URL,并且服务器将无法通过重定向到另一个URL来响应。

So this is not possible. 所以这是不可能的。 If you want to respond to https links, then you need an ssl certificate. 如果要响应https链接,则需要ssl证书。

RewriteCond %{HTTP:X-Forwarded-Proto} =https

Your code is correct. 你的代码是正确的。 Just put them inside the <VirtualHost *:443> 只需将它们放在<VirtualHost *:443>

Example: 例:

<VirtualHost *:443>
  SSLEnable

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

</VirtualHost>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM