【问题标题】:modRewrite RewriteCond to turn off ssl redirect for a certain requested URLmod_Rewrite RewriteCond 为某个请求的 URL 关闭 ssl 重定向
【发布时间】:2013-07-18 15:12:39
【问题描述】:

在我的网站 mycreditstatus.co.za 中,我使用 .htaccess 重写 URL 并将其从 http 重定向到 https,这是我在 public_html (http) 目录中用于 .htaccess 的代码:

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

它工作得很好,但它会将所有 http url 重写并重定向到 https。

问题是我的网站也会对非 https 网站执行一些请求,所以我不想重写并将某些链接重定向到 https。

这是我不想重定向/重写的链接之一:

http://imupost.co.za/

我想知道我应该为 public_ssl (https) 目录上的 .htaccess 编写的代码,因为请求将来自那里。

【问题讨论】:

  • 您可以在RewriteCond下使用排除项
  • 我不知道如何排除网址。使用 modRewrite 对我来说很新鲜。对不起。

标签: php regex .htaccess rewrite url-redirection


【解决方案1】:

试试这个代码:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^imupost.co.za$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

【讨论】:

  • 并在没有第三行的情况下尝试这个。你想说什么“代码但它仍然不起作用”?是有错误还是没有重定向?
  • 好的,我试试不加第三行。我的意思是,它仍然重定向到 (https://)imupost.co.za
  • 但这不是你想要的?你说你想将 http 重定向到 https 并且代码会这样做......
  • 对不起,如果我的问题不清楚。我有一个网站 mycreditstatus.co.za,它是一个 https 网站。它应该向其他网址发送请求,例如imupost.co.za,但它所做的是,它将 http 更改为 https,我只想删除对 imupost.co.za 的 https 重定向。
【解决方案2】:

将此代码放在 public_html 目录下的 .htaccess 中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^imupost\.co\.za$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

并将此代码放在 public_ssl 目录下的 .htaccess 中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^imupost\.co\.za$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

  • 关于你的代码的一个小问题,你为什么要在点之前添加反斜杠?
  • @LucasWillems:因为这是一个正则表达式,其中点表示任何字符。所以理论上正则表达式 imupost.co.za 可以匹配 imupost:co-za 也是我们不想要的。在任何正则表达式中,点都需要转义。
  • 非常感谢您的回答!
  • @anubhava:代码仍将 imupost.co.za 重定向到 https。 :(
  • @maikelsabido:请在其他浏览器中测试或清除浏览器缓存。
猜你喜欢
  • 1970-01-01
  • 2014-02-13
  • 2021-07-22
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
相关资源
最近更新 更多