替换一部分长URL并重定向

发布于 2025-01-18 19:31:26 字数 689 浏览 3 评论 0原文

有没有办法重定向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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2025-01-25 19:31:26

要替换no-slash在URL路径中仅由单个路径段组成的 somets ofers-else ,那么您可以使用mod-rewrite进行以下操作,靠近根部.htaccess文件。

RewriteEngine On

# Replace "no-slash" in URL-path with "something-else"
RewriteRule ^([\w-]+)no-slash([\w-]+)$ /$1something-else$2 [R=302,L]

假设URL路径只能由字符组成0-9azaz_((下划线)和-(连字符)。

$ 1$ 2 BackEferences在字符串之前和之后包含匹配的URL路径,以重复替换。

我尝试使用Rewriterule ^/no-slash/(.* friterulele ^/sosings-else/$ 1 [l]

之类的东西

您在URL中匹配 slashes - path-在您的示例中不会发生。在要替换的字符串之前,您也不允许任何东西(例如product-catgeory -)。

.htaccess上下文中,由Rewriterule staters 匹配的URL路径不是从斜线开始。 S,类似于^/no-slash的模式将从未匹配。


更新:

另一个示例。 example.com/demo-tools-for-construction-work因此,Word URL中的工具必须用设备>设备和工具。 /p>

(我假设这都应该是小写。)

您的第二个示例(在注释中)的一个问题是,工具也存在于目标网址中,因此这自然会导致无尽的重定向循环。

为了防止这种“循环”,您需要排除您正在重定向的URL。例如。您可以排除已经包含设备和工具的URL。

例如:

# Replace "tools" in URL-path with "equipment-and-tools"
# - except if it already contains "equipment-and-tools"
RewriteCond %{REQUEST_URI} !equipment-and-tools
RewriteRule ^([\w-]+)tools([\w-]+)$ /$1equipment-and-tools$2 [R=302,L]

condpattern 上的前缀(rewriteCond指令的第二个参数)否定了表达式。因此,在这种情况下,当设备和工具不包含在请求的URL中时,它将成功。

To replace no-slash with something-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.

RewriteEngine On

# Replace "no-slash" in URL-path with "something-else"
RewriteRule ^([\w-]+)no-slash([\w-]+)$ /$1something-else$2 [R=302,L]

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.

I tried to use stuff like RewriteRule ^/no-slash/(.*)$ /something-else/$1 [L]

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 the RewriteRule pattern does not start with a slash. S, a pattern like ^/no-slash will never match.


UPDATE:

another example. example.com/demo-tools-for-construction-work So word TOOLS in URL must be replaced with EQUIPMENT-AND-TOOLS.

(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:

# Replace "tools" in URL-path with "equipment-and-tools"
# - except if it already contains "equipment-and-tools"
RewriteCond %{REQUEST_URI} !equipment-and-tools
RewriteRule ^([\w-]+)tools([\w-]+)$ /$1equipment-and-tools$2 [R=302,L]

The ! prefix on the CondPattern (2nd argument to the RewriteCond directive) negates the expression. So, in this case it is successful when equipment-and-tools is not contained in the requested URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文