【问题标题】:How to redirect specific links to https:// everything else to http:// using .htaccess?如何使用 .htaccess 将特定链接重定向到 https:// 其他所有内容到 http://?
【发布时间】:2012-05-17 08:45:04
【问题描述】:

我正在尝试使用 htaccess 自动将所有对某些页面的请求定向到 https,将其他所有请求定向到 http://

这是我得到的代码来强制 ssl 在下面工作

RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]

这是按预期工作并重定向到这些页面的 ssl 版本。然后我尝试添加以下内容以将其他所有内容重定向到 http:

RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]

此代码在转到 /va-purchase-request /va-homebuyers-guide 和 /va-streamline-refinance 时会导致重定向循环,并且根本不会重定向其他页面。

我完全坚持这一点,任何帮助将不胜感激!

编辑:

我在 wordpress 添加的 .htaccess 文件中也有此代码...它会干扰其他重定向吗?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

#结束WordPress

【问题讨论】:

  • 检查this question。 (您尝试做的可能部分不安全......)

标签: apache .htaccess mod-rewrite redirect ssl


【解决方案1】:

用这个替换你的 .htaccess 代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

【讨论】:

  • 这正在将其他所有内容重定向回 http:但是,当我转到应该重定向的页面之一时,我会被发送到主页(domain.com)有什么想法吗?
  • 很抱歉没有找到你。您尝试了哪个 URI 不起作用?是否有可能在其他地方以及另一个 .htaccess 代码或代码内部存在一些重定向?
  • 感谢您回到我身边...当我使用您的代码时,网址 domain.com/va-homebuyers-guide domain.com/va-purchase-requestdomain.com/va-streamline-refinance 都指向 domain.com。 wordpress 添加的文件中还有一些其他重定向,如下所示,直接在您给我的代码下方 # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond % {REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule 。 /index.php [L] # 结束 WordPress
  • 为清楚起见,编辑了原始结果,添加了 wordpress 代码
  • 啊,没想到 Wordpress 也在现场。 WP 重定向规则肯定会干扰这里,特别是这个RewriteRule . /index.php [L]domain.com/va-homebuyers-guide 也是 WP 服务页面还是 WP 外部?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2019-03-18
  • 2012-10-11
  • 2015-11-10
  • 2018-08-31
相关资源
最近更新 更多