问题
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¶m=2
to
/slug/new_slug/slug?param=1¶m=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