php 使用 .htaccess 强制 http

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19230909/
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-08-25 19:03:58  来源:igfitidea点击:

Forcing http using .htaccess

php.htaccess

提问by Arian Faurtosh

This is the script I have right now, how do I have my script force all traffic to http, currently it is doing the exact opposite, it is forcing all traffic to https.

这是我现在拥有的脚本,我如何让我的脚本强制所有流量到 http,目前它正在做完全相反的事情,它强制所有流量到 https。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I've also tried this and it didn't work

我也试过这个,但没有用

RewriteEngine On
RewriteCond %{HTTP} !=on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I got this error:

我收到此错误:

Too many redirects occurred trying to open www.blankpage.com .

尝试打开 www.blankpage.com 时发生了太多重定向。

回答by Jon Lin

You want to check that HTTPS is on:

您想检查 HTTPS 是否开启

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

And if it is on (%{HTTPS} on), redirect to http://. There is no mod_rewrite variable called %{HTTP}, only %{HTTPS}which can be "on" or "off".

如果它在 ( %{HTTPS} on) 上,则重定向到http://. 没有名为 的 mod_rewrite 变量%{HTTP},只有%{HTTPS}它可以是“on”或“off”。

The reason why you were getting the too many redirects error is because:

您收到太多重定向错误的原因是:

RewriteCond %{HTTP} !=on

is always true no matter if the request is http or https, since the variable doesn't exist, it will neverbe equal to "on". Therefore, even if the request is http, you keep getting redirected to the same URL (http).

无论请求是 http 还是 https,它始终为真,因为该变量不存在,它永远不会等于“on”。因此,即使请求是 http,您也会不断被重定向到相同的 URL (http)。

回答by Abdulbasit

In the processwire htaccess look for these lines

在 processwire htaccess 中查找这些行

# -----------------------------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

If you only want to allow HTTPS, uncomment the RewriteCond and RewriteRule lines below.

如果您只想允许 HTTPS,请取消注释下面的 RewriteCond 和 RewriteRule 行

# -----------------------------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

   RewriteCond %{HTTPS} off
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

# -----------------------------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

If you only want to allow HTTP use the code below

如果您只想允许 HTTP 使用下面的代码

# -----------------------------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

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