【问题标题】:.htaccess rewrite rule for forcefully redirect https to http.htaccess 重写规则,用于强制将 https 重定向到 http
【发布时间】:2015-12-26 17:26:09
【问题描述】:

我有一个主域https://www.domain.comhttps://domain.com 和一个通配符DNS,如https://domain.com/index.php?name=abcd 强制重定向http://abcd.domain.com,但我还有另一个问题,我的登录页面是https://domain.com/login.php?name=abcd,我想要像强制http://domain.com/login.php?name=abcd 这样的结果但它不能在 HTTP 上重定向。

RewriteEngine On

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which request index.php
  RewriteCond %{REQUEST_URI}  ^/index\.php
#and contain a username paramater
  RewriteCond %{QUERY_STRING} ^(.*?)(?:^|&)name=([^&]+)(.*?)$
#then rewrite them to name.domain.com without the name parameter
  RewriteRule ^ http://%1.domain.com%{REQUEST_URI} [R,L]

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which was requested without https
  RewriteCond %{HTTPS} off
#then redirect to https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]




#Capture subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.(?:domain\.com)$
#If we're not working on www
RewriteCond %{HTTP_HOST} !^(?:www.domain\.com)$
#If querystring doesn't contain name
RewriteCond %{QUERY_STRING} !^(.*?)(?:^|&)name=([^&]+)(.*?)$
#Add username to querystring
RewriteRule index.php index.php?name=%1 [L,QSA]

这个 htaccess 代码工作正常,但我想在 htaccess 中登录脚本


在聊天中澄清后,这是我需要的:

应将所有 URL 重写为 https,但 login.php 除外,它应保留为 http。如果login.phphttps 出现,则将其重写为http

【问题讨论】:

  • 我阅读了您的 .htaccess,但我不明白您想要实现什么。 “我想在 htaccess 中登录脚本”是什么意思?
  • 我想说我想要这个结果domain.com/login.php?name=abcd 我的域是 https 所以它会自动重定向到 https
  • 你想要什么输入/login.php?name=abcd?是您现在拥有index.php 的地方,还是除了它或其他东西?
  • index.php 也在同一目录中
  • 我还是不明白,什么时候你想见login.php。重定向到 login.php 的 URL 或条件应该是什么?

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

你已经有了重写https的规则,剩下的就是排除login.php

RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/login\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

如果login.php 被请求 https,则将其重写为http

RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
RewriteCond %{HTTPS} on
RewriteRule ^login.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

最后,一个小提示:永远不要在启用301 的情况下进行测试,有关详细信息,请参阅此答案Tips for debugging .htaccess rewrite rules

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 我应该在哪里添加这些行
    • 只需添加到您站点的 vhost_ssl.conf 配置(或站点根目录下的 .htaccess)
    • 这一行表示 http 上的所有 url 重定向,但我只想要 http 上的 login.php 重定向
    • 也许可以试试这样的RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} login.php RewriteRule ^(.*)$ https://www.example.com/folder/login.php$1 [R,L]
    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2020-05-28
    • 2019-02-04
    相关资源
    最近更新 更多