问题
some bug in my online catalog SEF URL generation created a situation where some category slug is added to the URL twice. to fix this i would like to use .htaccess to remove the this part of the URL and have the whole URL shift up one level including URL's which also include a product under them.
the duplicate occurrence is of the sub-category painting-tools
under the category tools
so the wrong URL looks like this: /store/items/catalog/tools/painting-tools/painting-tools or /store/items/catalog/tools/painting-tools/painting-tools/{some product}
instead of being the correct URL like this:
/store/items/catalog/tools/painting-tools
or /store/items/catalog/tools/painting-tools/{some product}
and the URLs
/store/items/catalog/tools/painting-tools/
or /store/items/catalog/tools/painting-tools/{some product}
should actually be like this
/store/items/catalog/tools
or /store/items/catalog/tools/{some product}
tried using this rule, but its not working
RewriteRule ^/painting-tools/painting-tools/(.*) /painting-tools/$1 [QSA]
i think the ^ part is not correct since its not the beginning of the URL. How can i fix it ? thanks
回答1:
You can use this back-reference based redirect rule as very first rule in your .htaccess:
RewriteRule ^(.+?/([^/]+))/\2(/.*)?$ $1/$3 [NE,L,R=302]
RewriteRule ^(.+?/tools)/painting-tools(?:/.*)?$ $1/ [L,NC,R=302]
It is capturing repeat value after initial part and grouping it in this sub-pattern ([^/]+)
. Later in the regex it is using back-reference \2
to make sure same captured value is repeated.
Make sure you have proper RewriteBase
defined to either /
or /store/
wherever your htaccess is located.
Update: As per discussion below:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
This will remove /painting-tools2
from URLs.
来源:https://stackoverflow.com/questions/32456587/htaccess-remove-duplicate-part-of-a-url