简体   繁体   English

使用重定向 301 .htaccess 更改和删除 url 的一部分)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM