【问题标题】:htaccess 301 redirect removing html and numberhtaccess 301 重定向删除 html 和数字
【发布时间】:2016-06-09 08:59:09
【问题描述】:

我有一个棘手的重定向,希望您或其他人可以帮助我。

我需要的 301 重定向必须同时删除数字和“.html”,因此所有以数字结尾的 URL 和 html。

例子:

原网址:http://www.domain.com/articles/sampe/2015-05-14/post-name/11234566.html

新网址:http://www.domain.com/articles/sampe/2015-05-14/post-name/

所以如果URL末尾有数字和.html,它必须重定向。

提前谢谢你!

【问题讨论】:

    标签: .htaccess redirect mod-rewrite


    【解决方案1】:

    可能是这样的

    RewriteRule ^articles/sampe/([^/]+)/post-name/([0-9]+).html$ http://www.domain.com/articles/sampe/$1/post-name/ [R=301,L]
    

    这将 301 重定向所有 url 模式,例如 /articles/sampe/XXX/post-name/YYY.htmlhttp://www.domain.com/articles/sampe/XXX/post-name/

    编辑
    将上述规则扩展为适用于 url 中包含 post-name 的多个 url 模式:

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/post-name/([0-9]+).html$ http://www.domain.com/$1/$2/$3/post-name/ [R=301,L]
    

    这将获取您网站中的所有网址,例如:/*/*/*/post-name/*.htm 并将它们重定向到 /*/*/*/post-name/

    新的重定向将适用于所有网址,例如:
    /articles/somethingelse/2015-05-14/post-name/11234566.html
    /posts/category/2015-06-10/post-name/1589.html
    /demo/path/folder/post-name/999999.html

    任何看起来像:文件夹/文件夹/文件夹/post-name/number.html

    【讨论】:

    • 您好安德鲁,感谢您的快速回复!幸运的是(我认为)它只需要在最后重定向 NUMBER 和 HTLM,如果有的话,其余的都是可变的,并且很难为每个添加行。需要重写 */(thenumber).html,所以我认为更多的重写匹配,对于那些匹配任何数字和 .html = remove/redirect 并且只保留数字之前的所有内容。这有意义吗?
    • @TDM 好吧,如果您向我们提供一些不同的 url 模式,我们可以创建适当的规则。我发布的上述规则将适用于您的示例中的所有 url,在末尾没有 number.html 重定向它们
    • @TDM 检查我在回答中的编辑。它提供了一个更通用的解决方案。
    • 您好,感谢您的编辑!该解决方案基本上必须采用带有数字的任何 URL,所有 URL 的长度都会不同,因此 */01010101.html 将被重定向到 */,数字和 .html 组合是触发器/过滤器?
    • @TDM 是的。任何以 number.html 结尾的 url 模式都会受到影响。任意数字长度。编辑的规则有 2 个限制。 (a) /post-name/ 必须存在于 url 和 (b) url 中的最后一页必须只有数字作为名称和 .html 作为扩展名
    猜你喜欢
    • 2015-08-06
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    相关资源
    最近更新 更多