Forcing http using .htaccess

早过忘川 提交于 2021-02-05 20:10:44

问题


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.

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 .


回答1:


You want to check that HTTPS is on:

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".

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 never be equal to "on". Therefore, even if the request is http, you keep getting redirected to the same URL (http).




回答2:


In the processwire htaccess look for these lines

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

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

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

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

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

If you only want to allow HTTP use the code below

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

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


来源:https://stackoverflow.com/questions/19230909/forcing-http-using-htaccess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!