使用特定模式重定向数百个URL

时间:2017-11-17 19:14:00

标签: regex .htaccess

我不确定如何在.htaccess中正确编写指令以获取这样的URL:

www.domain.com/photo/weather/p1741-high-stress.html
www.domain.com/photo/cultural-practices/p1752-thermal-convection.html
www.domain.com/photo/turf-diseases/p1748-algae-and-viruses.html

为:

www.domain.com/gallery/detail/high-stress
www.domain.com/gallery/detail/thermal-convection
www.domain.com/gallery/detail/algae-and-viruses

基本上是这样的:

  • 将第一段从photo更改为gallery
  • 将第二段从任何更改为detail
  • 剥离第三段的第一部分(p#####-
  • 剥离最后的.html

2 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine on
RewriteRule ^photo/[^/]+/p\d+-(.+)\.html$ gallery/detail/$1 [NC,R=301,L]

答案 1 :(得分:0)

正则表达式匹配原始网址:

www\.domain\.com/photo/[^/]+/p\d+-(.+?)\.html

将其替换为:

www.domain.com/gallery/detail/$1