2011-11-29 151 views
5

我有一個重寫規則,它使用子域的值更改服務器變量。 這部作品subdomain.mydomain.nl/somethinghere但不能在subdomain.mydomain.nl在IIS 7.5中重寫服務器變量

<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的情況下,這樣做的輸出被「子域」。

在URL 2的情況下,這樣做的輸出爲「」。

URL 1的輸出是正確的,但URL 2的輸出應該是「子域」了。

我已經運行兩個請求的痕跡,他們都表明,規則被匹配和執行。

任何人都可以幫助我嗎?

回答

10

當您設置自定義服務器變量,你應該HTTP_啓動它。當您添加自己的頭,它應該HTTP_X_開始添加主機頭有X.開始

說實話,我真的不能解釋爲什麼它沒有工作在某些情況下HTTP_,但HTTP_它的工作原理在所有情況下,這也是如何記錄。

<rules> 
    <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+)\.testsite\.nl" /> 
      <add input="{SERVER_NAME}" pattern="^www\.testsite\.nl$" negate="true" /> 
      <add input="{SERVER_NAME}" pattern="^testsite\.nl$" negate="true" /> 
     </conditions> 
     <serverVariables> 
      <set name="HTTP_X_MYVARIABLE" value="{C:2}" /> 
     </serverVariables> 
     <action type="None" /> 
    </rule> 
</rules> 

現在,您可以echo $_SERVER["HTTP_X_MYVARIABLE"];得到子域名。

我還清理了您的條件正則表達式以逃避. s,並且還添加了$以使其確實匹配確切的域名。

+0

非常感謝您的回答,這就是解決方案。真奇怪,我無法找到任何記錄。 – user1071188

+2

要設置請求標頭,需要添加HTTP_。這是記錄[這裏](http://learn.iis.net/page.aspx/665/url-rewrite-module-20-configuration-reference/#Setting_Server_Variables)(請參閱「關於請求標題的注意事項」)。然而,我也困惑的是爲什麼設置請求標題總是有效,但設置服務器變量似乎並不總能奏效。 –

+0

謝謝你。你救了我的命! –