【问题标题】:challenging mod rewrite condition matching subdomain and no file extension具有挑战性的mod重写条件匹配子域并且没有文件扩展名
【发布时间】:2014-04-21 08:34:05
【问题描述】:

希望有人能帮我解决这个问题。

我有几个 CNAMES 指向 mod_pagespeed 分片的主域,如果没有这样的子域,我已经将 www 添加到域:

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

我有 mod_pagespeed 设置来自动分片文件资产,如下所示:

ModPagespeedShardDomain example.com static1.example.com,static2.example.com
ModPagespeedMapRewriteDomain example.com www.example.com
ModPagespeedMapOriginDomain localhost example.com

为了消除重复内容惩罚,我想301重定向非文件,路径只请求回主域:

static[1 or 2].example.com/varying/dir/structure -(301)-> 
  www.example.com/varying/dir/structure

但是我需要排除 URL 中有 file.ext 的任何情况,因此请求不会被重写。

static[1 or 2].example.com/varying/dir/structure/file.of.any.ext <--ignored

希望这足够描述性,非常感谢任何帮助,谢谢。

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect mod-pagespeed


    【解决方案1】:

    您可以将这样的规则用作您的第一条规则:

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]*)/?$ http://www.example.com/$1 [L,R=301]
    

    【讨论】:

    • 谢谢。这正是我所需要的。
    • 还有一个问题,这在请求到子域根目录的情况下似乎不起作用。 static1.example[/] 需要 301 重定向到 example.com 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多