htaccess rewrite slug in a url

淺唱寂寞╮ 提交于 2020-01-15 12:33:19

问题


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.

e.g.

http://www.example.com/slug/old_slug

to

http://www.example.com/slug/new_slug

or

/slug/old_slug/slug

to

/slug/new_slug/slug

or

/slug/old_slug/slug/slug

to

/slug/new_slug/slug/slug

or finally:

/slug/old_slug/slug?param=1&param=2

to

/slug/new_slug/slug?param=1&param=2

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.


回答1:


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]


来源:https://stackoverflow.com/questions/29127863/htaccess-rewrite-slug-in-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!