Apache 重写规则查询具有正则表达式并递归运行
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 mod_rewrite 分两步完成此操作。第一步是删除
/try/this/
之后的所有“+”,然后第二步是检查请求最初是否有“+”,但当前的 URI 没有。该规则将不断删除“+”号,直到没有为止。但我们只是通过重写引擎传递它。
此规则检查原始请求中是否包含带有“+”的 URL,如果有,请检查它们是否已全部删除。如果是这样,请结束所有重写并重定向浏览器:
您提到要重定向到不带“+”符号的 /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.
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:
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.