[英]Change and remove a part of url with redirect 301 (htaccess)
I converted a Joomla website to WordPress. Joomla article URLs have /blog/<RandomNumber>-slug
.我将 Joomla 网站转换为 WordPress。Joomla 文章网址有
/blog/<RandomNumber>-slug
。 For example:例如:
example.com/blog/164-postname
example.com/blog/214-anotherpostname
I need to remove /blog/164-postname
from URL so it redirects to example.com/postname
我需要从 URL 中删除
/blog/164-postname
,以便它重定向到example.com/postname
Also there are some product category URLs with this format:还有一些产品类别 URL 具有这种格式:
example.com/category/<RandomNumber>-slug
For example:例如:
example.com/category/12-catname
I want to redirect these urls by changing category
to product-category
and removing numbers and -
character so mentioned URL redirects to example.com/product-category/catname
with 301 code.我想通过将
category
更改为product-category
并删除数字来重定向这些网址,并且-
提到的字符 URL 使用 301 代码重定向到example.com/product-category/catname
。
Try the following at the top of your root .htaccess
file using mod_rewrite:使用 mod_rewrite 在根
.htaccess
文件的顶部尝试以下操作:
# Redirect "/blog/123-postname" to "/postname"
RewriteRule ^blog/\d+-([\w-]+)$ /$1 [R=301,L]
# Redirect "/category/123-catname" to "/product-category/catname"
RewriteRule ^(category)/\d+-([\w-]+)$ /product-$1/$2 [R=301,L]
The $1
and $2
backreferences contain the match from the parenthesised subpatterns in the preceding RewriteRule
pattern . $1
和$2
反向引用包含前面RewriteRule
模式中带括号的子模式的匹配项。
Test first with 302 (temporary) redirect to avoid potential caching issues.首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.