【问题标题】:Selectively redirect to a new domain using apache vhost configuration使用 apache vhost 配置有选择地重定向到新域
【发布时间】:2018-05-12 20:17:20
【问题描述】:

这是Redirect to a new domain using apache vhost configuration的后续问题

我想要来自

的请求

somedomain.com/loadproduct?product=dell-inspiron-15

被重定向到

someotherdomain.com/dell-inspiron-15

但选择性地针对某些列入白名单的产品。

例如产品:

  1. dell-inspiron-15。
  2. dell-inspiron-16。
  3. dell-inspiron-17

重定向到新的 URL,但对于我想使用当前路径本身的任何其他产品。

目前我的虚拟主机配置如下所示。它为查询参数中的所有产品重定向到某个其他域。

Listen 12567
NameVirtualHost *:12567

<VirtualHost *:12567>
    ServerName somedomain.com
    ProxyPreserveHost On

    RewriteEngine On
    RewriteCond %{QUERY_STRING} (?:^|&)product=([^&]+) [NC]
    RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]
</VirtualHost>

问题:

  1. 如上所述,如何选择性地重定向?
  2. 鉴于我列入白名单的产品列表很小(可能有 10 到 11 项),存储这些列入白名单的产品并在 vhost 中阅读以及如何阅读的理想场所是什么。

非常感谢这里的任何线索。

【问题讨论】:

  • 如果列表像您说的那么小,请直接在您的 VirtualHost 中配置它们。当 10-15 RewriteCond 会为您提供所需的内容并继续进行项目中的其他事情时,尝试做一些复杂的事情是没有用的。
  • 你能举个例子吗?
  • 我会给出一个答案,cmments中的代码很糟糕。

标签: apache mod-rewrite httpd.conf vhosts


【解决方案1】:

由于您的模型数量很少,您可以将它们列出来,如下所示:

RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-15) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-16) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-17) [NC]
RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]

默认情况下,RewriteCond 行被解释为每行之间的 AND 操作。因此,在触发 RewriteRule 之前,每个条件都必须为 TRUE。这里在行之间使用 OR 运算符。只要一个匹配,就可以了。

【讨论】:

猜你喜欢
  • 2018-05-08
  • 2018-05-12
  • 2020-04-17
  • 2021-06-16
  • 2016-03-24
  • 1970-01-01
  • 2017-06-23
  • 2015-01-30
  • 2012-05-13
相关资源
最近更新 更多