在 IIS 7.5 中重写服务器变量
我有一个重写规则,它用子域的值更改服务器变量。 这适用于 subdomain.mydomain.nl/somethinghere 但不适用于 subdomain.mydomain.nl
<rule name="Change code" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{SERVER_NAME}" pattern="(www\.)?(\w+)\.mydomain\.nl" />
<add input="{SERVER_NAME}" pattern="^www.mydomain.nl.*" negate="true" />
<add input="{SERVER_NAME}" pattern="^mydomain.nl.*" negate="true" />
</conditions>
<serverVariables>
<set name="MYVARIABLE" value="{C:2}" />
</serverVariables>
<action type="None" />
</rule>
我已经测试了 2 个网址: 1: subdomain.mydomain.nl/somethinghere 2:subdomain.mydomain.nl
我使用以下代码在 PHP 中检索该变量:
echo $_SERVER['MYVARIABLE'];
对于 URL 1,其输出是“subdomain”。
对于 URL 2,其输出为“”。
URL 1 的输出是正确的,但 URL 2 的输出也应该是“子域名”。
我已经对这两个请求进行了跟踪,它们都显示规则正在匹配和执行。
谁能帮助我吗?
I have a rewrite rule, which changes a server variable with the value of a subdomain.
This works on subdomain.mydomain.nl/somethinghere but not on subdomain.mydomain.nl
<rule name="Change code" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{SERVER_NAME}" pattern="(www\.)?(\w+)\.mydomain\.nl" />
<add input="{SERVER_NAME}" pattern="^www.mydomain.nl.*" negate="true" />
<add input="{SERVER_NAME}" pattern="^mydomain.nl.*" negate="true" />
</conditions>
<serverVariables>
<set name="MYVARIABLE" value="{C:2}" />
</serverVariables>
<action type="None" />
</rule>
I have tested 2 urls:
1: subdomain.mydomain.nl/somethinghere
2: subdomain.mydomain.nl
I retrieve the variable in PHP with the following code:
echo $_SERVER['MYVARIABLE'];
In case of URL 1, the output of this is "subdomain".
In case of URL 2, the output of this is "".
The output of URL 1 is correct, but the output of URL 2 should be "subdomain" too.
I have run a trace of both requests, and they both show that the rule is being matched and executed.
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置自定义服务器变量时,应以
HTTP_
开头。当您添加自己的标头时,它应该以HTTP_X_
开头,以添加以 X 开头的主机标头。说实话,我无法真正解释为什么它可以在没有
HTTP_
的情况下工作> 在某些情况下,但使用HTTP_
它可以在所有情况下工作,这也是它的记录方式。您现在可以使用
echo $_SERVER["HTTP_X_MYVARIABLE"];
获取子域名。我还清理了条件正则表达式以转义
.
,并添加了$
以使其真正匹配确切的域名。When you set a custom server variable, you should start it with
HTTP_
. When you add your own header, it should start withHTTP_X_
to add a host header starting with an X.To be honest, I can't really explain why it works without
HTTP_
in some scenarios, but withHTTP_
it works in all scenarios and that's also how it's documented.You can now get the subdomain name with
echo $_SERVER["HTTP_X_MYVARIABLE"];
.I've also cleaned up your conditional regular expressions to escape the
.
s and also added a$
to make it truly match the exact domain names.