【问题标题】:Using .htaccess I want to redirect based on http_referer all subdomains EXCEPT two specific subdomains使用 .htaccess 我想基于 http_referer 重定向所有子域,除了两个特定的子域
【发布时间】:2012-01-05 17:01:04
【问题描述】:

使用 .htaccess 我正在努力编写正确的正则表达式以基于 http_referer 重定向所有子域,除了两个。

假设我有

one.refered.com
two.refered.com

我不想重定向,而是 example.refered.com[any other subdomain but one or two].refered.com

为了得到它们,我可以使用

RewriteCond %{http_referer} ^http://([^.]+.)*(refered).com
RewriteRule ^$ yoursite.html [R=302,L]

但我在排除部分苦苦挣扎。

【问题讨论】:

    标签: regex .htaccess mod-rewrite regex-negation http-referer


    【解决方案1】:

    试试这个规则:

    RewriteCond %{HTTP_REFERER} ^http://(.+\.)*refered\.com
    RewriteCond %1 !^(one|two)\.$
    RewriteRule ^$ yoursite.html [R=302,L]
    
    • 在第一个条件下,我们捕获子域名
    • 在第二种情况下,我们检查捕获的子域名是否不等于one.two.

    如果两个条件都满足(条件之间的默认“关系”是逻辑AND),那么我们继续重写/重定向。

    测试数据:

    http://meow.refered.com/hello.php     = redirect
    http://one.refered.com/hello.php      = ignore
    http://one.two.refered.com/hello.php  = redirect
    

    【讨论】:

      【解决方案2】:

      这非常棘手,除非您必须将其提供给另一个工具(显然您这样做),否则我不建议您这样做。

      http://(?!one|two)(\w+\.)+(?<!one|two)referred\.com
      

      这说:

      查找“http://”,只要 at 后面没有“一”或“二”

      查找任意数量的子域 (\w.)+

      然后找到“referred.com”,只要它前面没有“一”或“二”

      http://one.referred.com                  * no match
      http://two.referred.com                  * no match
      http://blah.referred.com                 * matches
      http://blah.one.blah.referred.com        * matches
      http://two.blah.referred.com             * no match
      

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多