301 重定向 .htaccess - 将以 .html 结尾的 URL 转换为相同但转换为不以 .html 结尾的目录名称

301 Redirect .htaccess - convert URL ending in .html to same but convert to directory name without ending in .html

我非常感谢使用 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/ [R=301,L]

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

您可以尝试使用 mod_alias 代替:

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

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

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