I came across a weird issue while creating a few rewrite rules for my website which is hosted by GoDaddy. I'm able to create one rewrite rule using the {HTTP_HOST}
or {SERVER_NAME}
variables, but at the time of adding a second rule, it looks like the {HTTP_HOST}
/{SERVER_NAME}
doesn't return a value (I guess), so my code fails.
After a small mistake, I found a work around to the issue (which I will add at the bottom). I'm asking this question because I would like to:
- Understand the reason of this behavior
- Know if this is happening to someone else
- Find out if my approach is wrong or if I'm missing something for it to work
Due to this issue, I decided to clear my whole web.config file for testing purposes and have only the rewrite rules. I tried multiple changes, but I can't recall all of them at this time, so I will add as much as I remember.
Issue
This code works
<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>
This code doesn't (see notes inside code)
<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>
This is what I get in the "detail report"
404.0 Error - Details
- Module: IIS Web Core
- Notification: MapRequestHandler
- Handler: StaticFile
- Error Code: 0x80070002
- Requested URL: http://mySiteName.com:80/myFile.xml
- Physical Path: D:\Hosting\123123123\html\myFile.xml
- Logon Method: Anonymous
- Logon User: Anonymous
500.0 Error - Details
- Module: ServerSideIncludeModule
- Notification: ExecuteRequestHandler
- Handler: SSI-html
- Error Code: 0x80070002
- Requested URL: http://mySiteName.com:80/thisThingy.txt
- Physical Path: D:\Hosting\123123123\html\thisThingy.txt
- Logon Method: Anonymous
- Logon User: Anonymous
Now, if change {HTTP_HOST}
for a string in the second rule only, both rules start working:
<action type="Rewrite" url="/SiteDependentFiles/StaticFolderName/new_thisThingy.txt" appendQueryString="false"/>
Please note:
I also tried using a different patternSyntax
option, Redirect
instead of Rewrite
, stopProcessing="false"
, used {SERVER_NAME}
instead of {HTTP_HOST}
, added <clear />
before first rule, but nothing changes. Same behavior.
Workaround
Always add a rule condition with {HTTP_HOST}
to be able to use {SERVER_NAME}
as variable for rule's action
<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>
Please note:
- I'm using
{HTTP_HOST}
and{SERVER_NAME}
, but using only{HTTP_HOST}
could work. I can't test this right now.