使用htaccess将https重定向到非https服务器上的http

问题描述 投票:0回答:4

我的服务器未设置 HTTPS,但有些用户仍在 URL 中使用 HTTPS 进行浏览。这导致我的聊天应用程序无法正常工作。

我需要通过更改 htaccess 文件将聊天页面从 HTTPS 重定向到 HTTP。这些页面的 URL 为 /chat、/chat/index 和 chat/text。

我已经尝试了以下方法,但似乎无法使其工作:

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^chat$ http://%{HTTP_HOST}/$1 [R=301,L,NC]
RewriteRule ^chat/index$ http://%{HTTP_HOST}/$1 [R=301,L,NC]
RewriteRule ^chat/text$ http://%{HTTP_HOST}/$1 [R=301,L,NC]

我做错了什么?

apache .htaccess redirect mod-rewrite
4个回答
4
投票

如果您没有 ssl 证书或服务器上未启用 ssl,则无法将 https 重定向到 http。如果您没有 ssl 证书,您将收到 ssl 证书错误。您需要为您的域名购买 ssl 证书才能将 https 重定向到 http。

获得有效的ssl证书后,您可以使用以下规则将https://example.com/chat重定向到http://example.com/chat

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^chat http://example.com%{REQUEST_URI} [L,R=301]

1
投票

如果您的服务器上没有 HTTPS,则不要选择性,将所有内容重定向到 HTTP

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

0
投票

以下终于为我解决了问题:

将所有 https 和/或 www 请求重定向到 non-httpsnon-www 及其所有子目录。

https://example.com/           至 http://example.com/

https://www.example.com/http://example.com/

http://www.example.com/    至 http://example.com/

添加到我的 .htaccess 文件:

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

请注意,不要在 RewriteCond 中使用 “\.” 转义 "."

在我的共享主机上工作(令我惊讶的是,用“\.”转义“.”在我的主机上不起作用)


0
投票
重写引擎开启

RewriteCond %{HTTPS} 开启

重写规则 (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

© www.soinside.com 2019 - 2024. All rights reserved.