【问题标题】:Rewrite URL that matches a directory, has a specific HTTP query and does NOT have a specific HTTP query combination重写与目录匹配、具有特定 HTTP 查询但没有特定 HTTP 查询组合的 URL
【发布时间】:2021-09-22 05:47:57
【问题描述】:

我遇到了很多关于基于 HTTP 查询的重定向以及匹配部分 URL 的问题,但是 不是两者都

我的目录结构如下:

  • http://localhost/common_parent/.htaccess
  • http://localhost/common_parent/demo/themes/
  • http://localhost/common_parent/themes/?ajax=1

我的重写工作正常,只是它阻止了第二个列表项中的目录:

RewriteCond %{REQUEST_URI} !\.xml$
RewriteRule ^[^/].*/themes(.+) themes$1 [L]

不幸的是,demo/themes/?ajax=1themes/?ajax=1 都是通过ajax HTTP 查询请求的。但是,foo HTTP 查询仅请求 demo/themes/

  • http://localhost/common_parent/demo/themes/?ajax=1&foo=bar
  • http://localhost/common_parent/themes/?ajax=1

简而言之,我该如何重写我的rewrite 以仅在存在ajax http 查询时应用同时foo http 查询在场吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite friendly-url


    【解决方案1】:

    你可以像这样使用重写规则:

    # when ajax=<whatever> query parameter is present
    RewriteCond %{QUERY_STRING} (?:^|&)ajax= [NC]
    # when foo=<whatver> is not present
    RewriteCond %{QUERY_STRING} !(?:^|&)foo= [NC]
    # when URI is not ending with .xml
    RewriteCond %{REQUEST_URI} !\.xml$ [NC]
    # rewrite handler
    RewriteRule ^[^/].*/themes(.+) themes$1 [L]
    

    【讨论】:

    • 是的,我已经开始搞乱%{QUERY_STRING},不幸的是,这破坏了访问themes/?ajax= 的脚本。我将结尾更改为(?:^|&amp;)ajax(.+) [NC],尽管它只是挂起。想法?
    • 重写规则会产生不正确的重写,但它永远不会挂起。在 chrome 开发工具中检查正在发送到 Web 服务器的 URL 以及返回到浏览器的响应。
    • 我默认使用 Waterfox Classic,Chrome 会根据需要处理重写,尽管 Waterfox 没有。我也尝试过 Firefox,没有发送/接收完全相同的 URL 请求的标头。
    • 没关系,这是一个 SSE 请求,我验证它有效。我将线路更改为RewriteCond %{QUERY_STRING} (?:^|&amp;)ajax= [NC]。谢谢,够近了!
    • 答案已相应编辑。很高兴它成功了。
    猜你喜欢
    • 1970-01-01
    • 2015-03-25
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2017-08-19
    • 1970-01-01
    相关资源
    最近更新 更多