【问题标题】:Creating variable in apache config在 apache 配置中创建变量
【发布时间】:2018-07-27 01:21:11
【问题描述】:

我有一个类似下面的 apache 配置:

RewriteCond %{QUERY_STRING} ^site=(eu|jp|in)$ [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in)(?:&|$) [NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstPage/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in) [NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/secondPage/%1? [R=301,L,NC]

我可以将“(eu|jp|in)”存储在某个变量中并在所有三个重写规则中重用该变量吗?

感谢这里的领导?

【问题讨论】:

    标签: apache .htaccess mod-rewrite httpd.conf


    【解决方案1】:

    (eu|jp|in) 实际上是一个正则表达式。您不能真正将该部分存储在变量中,但您可以使用 env 变量来避免重复与此相同的条件:

    # set env var QS if quesry string has site=(eu|jp|in)
    RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in)(?:&|$) [NC]
    RewriteRule ^ - [E=QS:%1]
    
    # now use value stored in QS variable
    RewriteCond %{ENV:QS} ^(.+)$
    RewriteRule ^/?fetchHomePage\.action$ https://example.com/%1? [R=301,L,NC]
    
    RewriteCond %{ENV:QS} ^(.+)$
    RewriteRule ^/?fetchFirstPage\.action$ https://example.com/firstPage/%1? [R=301,L,NC]
    
    RewriteCond %{ENV:QS} ^(.+)$
    RewriteRule ^/?fetchSecondPage\.action$ https://example.com/secondPage/%1? [R=301,L,NC]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-04
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多