问题描述
我的在线目录SEF URL生成中的一些错误造成了以下情况:将某些类别的标签两次添加到URL.要解决此问题,我想使用.htaccess删除URL的这一部分,并使整个URL向上移动一个级别,包括URL,该URL下还包括一个产品.
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.
重复出现的是类别 tools
所以错误的URL看起来像这样:/store/items/catalog/tools/painting-tools/painting-tools或者/store/items/catalog/tools/painting-tools/painting-tools/{某些产品}
so the wrong URL looks like this: /store/items/catalog/tools/painting-tools/painting-toolsor /store/items/catalog/tools/painting-tools/painting-tools/{some product}
不是这样的正确网址:
/store/items/catalog/tools/painting-tools
或/store/items/catalog/tools/painting-tools/{某些产品}
or /store/items/catalog/tools/painting-tools/{some product}
和网址
/store/items/catalog/tools/painting-tools/
或/store/items/catalog/tools/painting-tools/{某些产品}
or /store/items/catalog/tools/painting-tools/{some product}
实际上应该是这样
/store/items/catalog/tools
或/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]
我认为^部分不正确,因为它不是URL的开头.我该如何解决?谢谢
i think the ^ part is not correct since its not the beginning of the URL.How can i fix it ?thanks
推荐答案
您可以在.htaccess中将此基于反向引用的重定向规则用作第一条规则:
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]
正在捕获初始部分之后的重复值,并将其分组到此子模式([^/] +)
中.稍后在正则表达式中,它使用向后引用 \ 2
来确保重复捕获相同的值.
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.
确保您已正确定义/
或/store/
的htaccess所在的正确的 RewriteBase
.
Make sure you have proper RewriteBase
defined to either /
or /store/
wherever your htaccess is located.
更新:根据以下讨论:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
这将从网址中删除/painting-tools2
.
这篇关于.htaccess删除网址的重复部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!