apache - 仅当在 RewriteMap 中找到模式时才需要 mod_rewrite 进行重定向

标签 apache mod-rewrite redirect url-rewriting

我需要使用 mod_rewrite 来检查某个模式是否在 txt 文件中,并且仅在找到时才重定向。我想根据 url 是否包含在 map 文件中找到的用户 ID 将其重定向到启动页面。

所有用户 ID 都符合相同的模式,因此我匹配该模式并将其提供给我的映射。假设用户 ID 可能如下所示

12ab-34bc-5601

模式匹配和后续重定向工作正常。如果找到匹配,用户将被重定向到我的启动页面。但是,如果在 map 中找不到匹配项,它们将被重定向到原始请求。即使我使用 L 标志,这也会创建一个重定向循环。

RewriteEngine on
RewriteMap UserBlockMap txt:/home/username/users.map
RewriteRule <user id pattern> ${UserBlockMap:$3|$1} [R,L,NE]

我可以做些什么来打破这个重定向循环,还是 RewriteMap 不是我想在这里使用的工具?

最佳答案

我之前没有意识到,但是 RewriteRule 可以返回并检查 RewriteCond。我找到的解决方案是这样的。

RewriteCond ${UserBlockMap:$3}  !^$  [NC]
RewriteRule <user id pattern> ${UserBlockMap:$3|$1} [R,L,NE]

基于这篇博文中概述的内容。 http://www.jeremytunnell.com/posts/mod_rewrite-attempting-to-bend-rewritemap-rewritecond-and-rewriterule-to-my-will

关于apache - 仅当在 RewriteMap 中找到模式时才需要 mod_rewrite 进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9264877/

相关文章:

regex - 重写 URL 并使新 URL 显示在地址栏中

security - 我的网站被黑了,htaccess 文件被泄露了,它应该是什么样子的?

apache - mod_rewrite 在子目录中

apache - RewriteRule 使 OAuth 授权失败

regex - ModRewrite 语句重定向到用户配置文件

authentication - 登录后Grails 3重定向

Apache:按 session 进行负载平衡

regex - Htaccess 重定向 Laravel 4

python - 如何使用Python检查url是否重定向到另一个url

apache - URL 重写和参数 GET 问题