我在为我的由 GoDaddy 托管的网站创建一些重写规则时遇到了一个奇怪的问题。我可以使用{HTTP_HOST}
or{SERVER_NAME}
变量创建一个重写规则,但是在添加第二个规则时,看起来{HTTP_HOST}
/{SERVER_NAME}
没有返回值(我猜),所以我的代码失败了。
在一个小错误之后,我找到了解决该问题的方法(我将在底部添加)。我问这个问题是因为我想:
- 了解这种行为的原因
- 知道这是否发生在其他人身上
- 找出我的方法是否错误,或者我是否遗漏了一些东西以使其正常工作
由于这个问题,我决定清除我的整个 web.config 文件以进行测试,并且只有重写规则。我尝试了多项更改,但目前我无法全部记住,所以我会根据我记得的情况添加。
问题
此代码有效
<system.webServer>
<rewrite>
<rules>
<rule name="Mask myFile.xml" stopProcessing="true" patternSyntax="Wildcard">
<!-- This rule always -->
<match url="*" />
<conditions>
<add input="{REQUEST_URI}" pattern="/myFile.xml" />
</conditions>
<action type="Rewrite" url="/SiteDependentFiles/{HTTP_HOST}/new_myFile.xml" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
此代码没有(请参阅代码内的注释)
<system.webServer>
<rewrite>
<rules>
<rule name="Mask myFile.xml" stopProcessing="true" patternSyntax="Wildcard">
<!-- This rule works on and off. When it doesn't work, it returns a 404 (Not found) error -->
<match url="*" />
<conditions>
<add input="{REQUEST_URI}" pattern="/myFile.xml" />
</conditions>
<action type="Rewrite" url="/SiteDependentFiles/{HTTP_HOST}/new_myFile.xml" appendQueryString="false"/>
</rule>
<rule name="Mask thisThingy.txt" stopProcessing="true" patternSyntax="Wildcard">
<!-- This rule never works and it returns a 500 (Internal Server) error -->
<match url="*" />
<conditions>
<add input="{REQUEST_URI}" pattern="/thisThingy.txt" />
</conditions>
<action type="Rewrite" url="/SiteDependentFiles/{HTTP_HOST}/new_thisThingy.txt" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
这就是我在“详细报告”中得到的
404.0 错误 - 详细信息
- 模块: IIS Web 核心
- 通知: MapRequestHandler
- 处理程序:静态文件
- 错误代码: 0x80070002
- 请求的 URL: http://mySiteName.com:80/ myFile.xml
- 物理路径: D:\Hosting\123123123\html\myFile.xml
- 登录方式:匿名
- 登录用户:匿名
500.0 错误 - 详细信息
- 模块: ServerSideIncludeModule
- 通知: ExecuteRequestHandler
- 处理程序: SSI-html
- 错误代码: 0x80070002
- 请求的 URL: http://mySiteName.com:80/ thisThingy.txt
- 物理路径: D:\Hosting\123123123\html\thisThingy.txt
- 登录方式:匿名
- 登录用户:匿名
现在,如果仅{HTTP_HOST}
在第二条规则中更改字符串,则两条规则都开始起作用:
<action type="Rewrite" url="/SiteDependentFiles/StaticFolderName/new_thisThingy.txt" appendQueryString="false"/>
请注意:
我还尝试使用不同的patternSyntax
选项,Redirect
而不是Rewrite
, stopProcessing="false"
,使用{SERVER_NAME}
而不是{HTTP_HOST}
,在第一条规则之前添加<clear />
,但没有任何变化。相同的行为。
解决方法
始终添加规则条件,{HTTP_HOST}
以便能够{SERVER_NAME}
用作规则操作的变量
<system.webServer>
<rewrite>
<rules>
<clear />
<!--
ALWAYS ADD <add input="{HTTP_HOST}" pattern="*" /> TO BE
ABLE TO USE {SERVER_NAME} MULTIPLE TIMES. (GODADDY ISSUE)
-->
<rule name="Mask myFile.xml" stopProcessing="true" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="*" />
<add input="{REQUEST_URI}" pattern="/myFile.xml" />
</conditions>
<action type="Rewrite" url="/SiteDependentFiles/{SERVER_NAME}/new_myFile.xml" appendQueryString="false"/>
</rule>
<rule name="Mask thisThingy.txt" stopProcessing="true" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="*" />
<add input="{REQUEST_URI}" pattern="/thisThingy.txt" />
</conditions>
<action type="Rewrite" url="/SiteDependentFiles/{SERVER_NAME}/new_thisThingy.txt" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
请注意:
- 我正在使用
{HTTP_HOST}
and{SERVER_NAME}
,但 using only{HTTP_HOST}
可以工作。我现在无法测试这个。