【问题标题】:use environment variables set in apache v-host to set query param使用 apache v-host 中设置的环境变量来设置查询参数
【发布时间】:2016-02-22 19:25:48
【问题描述】:

我们在 Apache vhost 中设置了一个环境变量 CODE

我想映射传入的请求

^/myresource?paramOne=one&paramTwo=two

/some-path-to/my-resource/resource.php?paramOne=one&paramTwo=two&code=theEnvCode

在 vhost RewriteRule 中,有没有办法将“code”查询参数设置为 env var CODE 的值,并从传入请求中传递所有其他查询参数?:

RewriteRule ^/my-resource /some-path-to/my-resource/resource.php [QSA,L]

Setenv CODE theEnvCode

编辑

一种可能性是硬编码“code”参数,请求中的任何其他参数都将被附加。但问题是如何保持同步 代码参数和环境变量CODE

RewriteRule ^/my-resource /some-path-to/my-resource/resource.php?code=theEnvCode [QSA,L]

Setenv CODE theEnvCode

/my-resource?one=two&three=four 的请求将映射到/some-path-to/my-resource/resource.php?code=theEnvCode&one=two&three=four

【问题讨论】:

  • 不,您不能为此使用 setenv。但是 mod_rewrite 可以设置环境变量:RewriteRule .* - [E=theEnvCode:$1]-kind of thing。
  • 感谢@MarcB,在 RewriteRule 中有没有办法将“代码”参数设置为硬编码,但仍然从传入请求(QSA 事物)传递任何其他查询参数?
  • rewriterule 基本上只是一个正则表达式搜索/替换。如果你想要硬编码的东西,那就硬编码吧……
  • 谢谢@MarcB,你介意给我一个上面描述的例子吗?我仍在使用 apache....
  • RewriteRule ^/my-resource /some-path-to/my-resource/resource.php?code=defaultcode [QSA, L] -- 你是这个意思吗?

标签: apache vhosts


【解决方案1】:

由于您重写而不是重定向到另一台服务器,因此您的 env 变量在目标 URL 中仍然可用。为什么不在 PHP 脚本中使用 apache_getenv() 检索它?无需将其作为 GET 参数传递。 见http://php.net/manual/en/function.apache-getenv.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2016-09-30
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多