替换一部分长URL并重定向
有没有办法重定向URL,如下所示:
URL是基于过滤系统生成的,所以它是这样的
https://example.com/product-category-no-slash-generated-part-is-autoadded-here
由于产品数量庞大,我不可能更改所有生成的URL-s,但我需要更改,因为例如,只有 no-slash
部分到 something-else
,因此重定向会执行以下操作:
旧 URL:
https://example.com/product-category-no-slash- generated-part-is-autoadded-here
新网址:
https://example.com/product-category-something-else- generated-part-is-autoadded-here
我希望我能够解释这个问题。
我尝试使用诸如 RewriteRule ^/no-slash/(.*)$ /something-else/$1 [L]
之类的东西,但我认为这不能满足我的需要。
Is there a way to redirect the URL as follows:
URL is generated based on a filtering system so it is like this
https://example.com/product-category-no-slash-generated-part-is-autoadded-here
Due to the massive product number, it is impossible for me to change all generated URL-s but I need to change, for example, only no-slash
part to something-else
, so redirect does this:
Old URL:
https://example.com/product-category-no-slash-generated-part-is-autoadded-here
New URL:
https://example.com/product-category-something-else-generated-part-is-autoadded-here
I hope I managed to explain the problem.
I tried to use stuff like RewriteRule ^/no-slash/(.*)$ /something-else/$1 [L]
but I think this does not work for what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要替换
no-slash
用在URL路径中仅由单个路径段组成的
somets ofers-else ,那么您可以使用mod-rewrite进行以下操作,靠近根部.htaccess
文件。假设URL路径只能由字符组成
0-9
,az
,az
,_
((下划线)和-
(连字符)。$ 1
和$ 2
BackEferences在字符串之前和之后包含匹配的URL路径,以重复替换。您在URL中匹配 slashes - path-在您的示例中不会发生。在要替换的字符串之前,您也不允许任何东西(例如
product-catgeory -
)。在
.htaccess
上下文中,由Rewriterule
staters 匹配的URL路径不是从斜线开始。 S,类似于^/no-slash
的模式将从未匹配。更新:
(我假设这都应该是小写。)
您的第二个示例(在注释中)的一个问题是,
工具
也存在于目标网址中,因此这自然会导致无尽的重定向循环。为了防止这种“循环”,您需要排除您正在重定向的URL。例如。您可以排除已经包含
设备和工具
的URL。例如:
!
condpattern 上的前缀(rewriteCond
指令的第二个参数)否定了表达式。因此,在这种情况下,当设备和工具
是不包含在请求的URL中时,它将成功。To replace
no-slash
withsomething-else
in the URL-path that only consists of a single path-segment then you can do something like the following using mod-rewrite, near the top of the root.htaccess
file.This assumes the URL-path can only consist of the characters
0-9
,a-z
,A-Z
,_
(underscore) and-
(hyphen).The
$1
and$2
backreferences contain the matched URl-path before and after the string to replace repsectively.In this you are matching slashes in the URL-path - which do not occur in your example. You are also not allowing for anything before the string you want to replace (eg.
product-catgeory-
).In a
.htaccess
context, the URL-path matched by theRewriteRule
pattern does not start with a slash. S, a pattern like^/no-slash
will never match.UPDATE:
(I'm assuming this should all be lowercase.)
A problem with your second example (in comments) is that
tools
also exists in the target URL, so this would naturally result in an endless redirect loop.To prevent this "loop" you would need to exclude the URL you are redirecting to. eg. You could exclude URLs that already contain
equipment-and-tools
.For example:
The
!
prefix on the CondPattern (2nd argument to theRewriteCond
directive) negates the expression. So, in this case it is successful whenequipment-and-tools
is not contained in the requested URL.