【问题标题】:htaccess redirect to https for an already Re-Written URLhtaccess 重定向到 https 以获得已经重写的 URL
【发布时间】:2015-01-21 20:36:51
【问题描述】:

这是我的 htaccess

Options +FollowSymLinks
RewriteEngine On

# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} register
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

# don't do anything for images/css/js etc
RewriteRule \.(gif|jpe?g|png|css|js|woff|map)$ - [NC,L]

# redirect to HTTP for all other pages
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !register
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [PT,L]

我有一个注册页面,其 URL 类似于 hxxp://www.example.com/index.php?register,并且 htaccess 正确重定向到 HTTPS。

现在我还希望我的购买页面 (hxxpp://www.example.com/buy)重定向到 HTTPS。此页面的真实 URL 是 hxxp://www.example.com/index.php?buy

请告诉我如何修改我的 htaccess 规则来满足这个需求。

【问题讨论】:

  • 您的 URI 方案有点混乱。一方面你有/index.php?register,另一方面你有/buy(内部重写为/index.php?products&type=buy。为什么不也有/register(重写为/index.php?register)?
  • @JustinIurman:抱歉,这是 URL 中的拼写错误。固定。

标签: regex apache .htaccess mod-rewrite redirect


【解决方案1】:

您可以在 https 规则中使用正则表达式替换:

# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(register|buy)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

确保这是我们 htaccess 中的第一条规则

【讨论】:

  • 之前测试过。不工作。该站点由干净的 URL 以及带有多个查询的长 URL 组成。 example.com/buy 也是一个已经重写的 URL,我希望它是 HTTPS。所以我认为 {QUERY_STRING} 在那种情况下不起作用。如果我错了,请纠正我。
  • 检查更新的规则并确保这是我们htaccess中的第一条规则
  • 是的,它正在工作。但我也想使用 QUERY_STRING 条件。我可以使用这个: RewriteCond %{HTTPS} off RewriteCond %{THE_REQUEST} /buy [OR] RewriteCond %{QUERY_STRING} register RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
  • 是的,你当然可以使用它,但要确保这条规则是第一条规则。
  • 将我的 htaccess 更改为 pastebin.com/YqME6PS2 不工作。不确定我的 OR 条件是否正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多