【问题标题】:Handle one query parameter specifically专门处理一个查询参数
【发布时间】:2021-07-29 16:09:02
【问题描述】:

我需要专门处理一个查询参数。 网址可能如下所示:

https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld

重定向应该指向

https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld

如您所见,app-Parameter 需要用作 URL 部分;其他的需要像传统的查询参数一样使用。
我已经想通了,如何实现第一部分,其中https://example.com/?app=egov 使用以下内容指向https://example.com/contextName/resources/apps/egov/index.html

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]

但我正在努力如何实现对其他参数的正确处理。

【问题讨论】:

    标签: regex apache redirect mod-rewrite parameters


    【解决方案1】:

    所有这些查询字符串的组合都可以通过一个规则来处理,就像这样:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
    RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
    

    RewriteCond 匹配app 查询参数并在%1 中捕获它,而& 之后的其余查询字符串在%2 中捕获。

    【讨论】:

      【解决方案2】:

      使用您显示的示例,请尝试遵循 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

      RewriteEngine ON
      ####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
      ##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
      RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
      RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
      
      ##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
      RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
      RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
      
      ##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
      RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
      RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
      

      你所有的 URLs uri 部分都是 NULL,所以规则是基于它编写的。

      【讨论】:

        猜你喜欢
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        相关资源
        最近更新 更多