【问题标题】:301 Redirect .htaccess - convert URL ending in .html to same but convert to directory name without ending in .html301 重定向 .htaccess - 将以 .html 结尾的 URL 转换为相同但转换为不以 .html 结尾的目录名称
【发布时间】:2015-03-05 13:59:44
【问题描述】:

我非常感谢使用 Apache .htaccess 进行以下 301 重定向的帮助,因为我一直不知道如何执行此操作:

我需要转换: http://blog.domain.com/2015/03/filename.html

到: http://www.domain.com/blog/filename/

原始文件结构有很多事情要做。构成原始 URL 的年份和月份,每个月和几年都会更改并返回。即 2015/03、2015/02、2015/01、2014/12 等...

结尾 filename.html 需要是新目录名称的名称,不带结尾 .html 并添加结束 '/'

这是我目前所拥有的,但它不起作用:

RewriteRule http://blog.domain.com/([0-9]+)/([0-9]+)/(.*)\.html$ http://www.domain.com/blog/$3 [R=301,L]

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您不能在重写规则的正则表达式中包含 URL 的 http://domainname/ 部分,只有 2015/03/filename.html 部分用于任何匹配。

    您可以尝试改用 mod_alias:

    RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([^/.]+)\.html$ http://www.domain.com/blog/$1/
    

    或者如果你已经有重写规则,最好坚持使用 mod_rewrite:

    RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
    RewriteRule [0-9]{4}/[0-9]{2}/([^/.]+)\.html$ http://www.domain.com/blog/$1/ [L,R=301]
    

    【讨论】:

    • 非常感谢。它工作得很好。对于其他人,我使用了 RewriteCond 等的第二个建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多