使用重定向 301 (htaccess) 更改和删除 url 的一部分
Change and remove a part of url with redirect 301 (htaccess)
我将 Joomla 网站转换为 WordPress。 Joomla 文章 URL 有 /blog/<RandomNumber>-slug
。例如:
example.com/blog/164-postname
example.com/blog/214-anotherpostname
我需要从 URL 中删除 /blog/164-postname
,以便它重定向到 example.com/postname
还有一些产品类别 URL 具有这种格式:
example.com/category/<RandomNumber>-slug
例如:
example.com/category/12-catname
我想通过将 category
更改为 product-category
并删除上面提到的数字和 -
字符来重定向这些网址 URL 使用 301 重定向到 example.com/product-category/catname
代码。
使用 mod_rewrite 在根 .htaccess
文件的顶部尝试以下操作:
# Redirect "/blog/123-postname" to "/postname"
RewriteRule ^blog/\d+-([\w-]+)$ / [R=301,L]
# Redirect "/category/123-catname" to "/product-category/catname"
RewriteRule ^(category)/\d+-([\w-]+)$ /product-/ [R=301,L]
</code> 和 <code>
反向引用包含前面 RewriteRule
模式 .
中带括号的子模式的匹配项
首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。
我将 Joomla 网站转换为 WordPress。 Joomla 文章 URL 有 /blog/<RandomNumber>-slug
。例如:
example.com/blog/164-postname
example.com/blog/214-anotherpostname
我需要从 URL 中删除 /blog/164-postname
,以便它重定向到 example.com/postname
还有一些产品类别 URL 具有这种格式:
example.com/category/<RandomNumber>-slug
例如:
example.com/category/12-catname
我想通过将 category
更改为 product-category
并删除上面提到的数字和 -
字符来重定向这些网址 URL 使用 301 重定向到 example.com/product-category/catname
代码。
使用 mod_rewrite 在根 .htaccess
文件的顶部尝试以下操作:
# Redirect "/blog/123-postname" to "/postname"
RewriteRule ^blog/\d+-([\w-]+)$ / [R=301,L]
# Redirect "/category/123-catname" to "/product-category/catname"
RewriteRule ^(category)/\d+-([\w-]+)$ /product-/ [R=301,L]
</code> 和 <code>
反向引用包含前面 RewriteRule
模式 .
首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。