Apache 重写规则查询具有正则表达式并递归运行

发布于 2024-12-14 14:03:11 字数 558 浏览 1 评论 0原文

我是 apache 重写规则的新手。 我正在尝试重写 URL

例如:

http://www.example .com/try/this/all+letters+with+plus -> http://www.example.com/try/this/allletterswithplus

所以要求是我必须将其重定向到包含 /try/this 的 URL,其余部分不包括 + 号。需要注意的一点是,加号的数量不是固定的,可以有2到7个,也可以有更多个加号。最好,带有正则表达式的选项会很好,因为 all+letters+with+plus 文本可以包含从数字到字母的任何内容,但没有迹象表明

我尝试了某种组合,但随后它进入了递归循环。

任何帮助将不胜感激。

I am new to apache rewrite rules.
I am trying to rewrite a URL

For ex :

http://www.example.com/try/this/all+letters+with+plus -> http://www.example.com/try/this/allletterswithplus

so the requirement is I have to redirect it to URL having /try/this and the rest excluding + sign. One point to note, the number of plus sign is not fixed, it can have 2 to 7 or may be more plus signs. Preferably, an option with regex will be good because the all+letters+with+plus text can include anything from number to alphabets but no signs

I tried some combination, but then it went into recursive loop.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷路的信 2024-12-21 14:03:11

您可以使用 mod_rewrite 分两步完成此操作。第一步是删除 /try/this/ 之后的所有“+”,然后第二步是检查请求最初是否有“+”,但当前的 URI 没有。

该规则将不断删除“+”号,直到没有为止。但我们只是通过重写引擎传递它。

RewriteRule ^try/this/(.*)\+(.*)$ /try/this/$1$2 [PT]

此规则检查原始请求中是否包含带有“+”的 URL,如果有,请检查它们是否已全部删除。如果是这样,请结束所有重写并重定向浏览器:

RewriteCond %{THE_REQUEST} \ /try/this/.*\+.*\ HTTP
RewriteRule ^try/this/([^\+]*)$ /try/this/$1 [R,L]

您提到要重定向到不带“+”符号的 /try/this/ URL。但是,如果您只想在内部重写它而不重定向浏览器,则不需要第二部分,只需第一条规则即可删除它们。

You can do this in 2 steps using mod_rewrite. The first step is to remove all of the "+" from anything after /try/this/, then the second step is to check if the request originally had "+" but the current URI doesn't.

This rule will keep removing "+" signs until there aren't any. But we're just passing this through the rewrite engine.

RewriteRule ^try/this/(.*)\+(.*)$ /try/this/$1$2 [PT]

This rule checks if the original request had a URL with "+" in it, and if it does, check to see if they've all been removed. If so, end all the rewriting and redirect the browser:

RewriteCond %{THE_REQUEST} \ /try/this/.*\+.*\ HTTP
RewriteRule ^try/this/([^\+]*)$ /try/this/$1 [R,L]

You mentioned that you wanted to redirect to the /try/this/ URL without the "+" signs. But if you just want to rewrite it internally and not redirect the browser, you don't need the 2nd part, just the first rule will remove them.

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