htaccess将url中的段塞重写 [英] htaccess rewrite slug in a url
问题描述
我已经搜索并搜索了有关重写规则的帮助,但似乎没有什么与我要尝试的工作特别匹配,希望有人可以提供帮助?
I've searched and searched for help on a rewrite rule but nothing seems to match specifically what I'm trying to do, hopefully someone can help?
我正在尝试替换网址中的某个段,该段之前和之后可能会有其他各种段.
I'm trying to replace a slug in a url that could have various other possible slugs before and after it.
例如
http://www.example.com/slug/old_slug
到
http://www.example.com/slug/new_slug
或
/slug/old_slug/slug
到
/slug/new_slug/slug
或
/slug/old_slug/slug/slug
到
/slug/new_slug/slug/slug
或最后:
/slug/old_slug/slug?param=1¶m=2
到
/slug/new_slug/slug?param=1¶m=2
如果这听起来令人困惑,则它们是类别中带有各种可能的url参数的产品,而我必须重命名产品,这意味着我想重写SEO和入站链接的旧url.
If that sounds confusing these are products in categories with various possible url parameters after them and I'm having to rename a product that means I want to rewrite the old urls for SEO and inbound links.
推荐答案
您可以在DOCUMENT_ROOT/.htaccess
文件中使用以下代码:
You can use this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/old_slug(/.*)?$ /$1/new_slug$2 [L,NE,R=302]
这篇关于htaccess将url中的段塞重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!