3
votes

I'm using IIS URL rewriting module to mask my internal URLs with friendly URLs through rewrite maps and Rewrite rules (not redirection). This is my rewrite map:

<rewriteMap name="HashTest">
     <add key="/nohash" value="/nohash.aspx" />
     <add key="/hash1" value="/hashtest.aspx#hash1" />
</rewriteMap>

and this is my rewrite rule:

<rule name="Rewrite rule1 for HashTest">
       <match url=".*" />
       <conditions>
           <add input="{HashTest:{REQUEST_URI}}" pattern="(.+)" />
       </conditions>
       <action type="Rewrite" url="{C:1}" />
</rule>

This is working for URLs with no hashtags, so every time I query www.mysite.com/nohash it shows me content from www.mysite.com/nohash.aspx with our changing the URL on the browser.

Now when I try to rewrite to a URL containing a hashtag I get a 404 error, for instance www.mysite.com/hash1 should just show me content from /hashtest.aspx#hash1 but I just get a 404.

Now if I change my rule action type to Redirect it does make redirection successfully, so I don't know why it doesn't work with rewrite.

I know hashtags are not sent to the server on the request, but it would make sense if my rewrite map was backwards, like <add key="/hashtest.aspx#hash1" value="/hash1" />.

Any insights on why redirection works with hastags but rewrite doesn't?. I'm not married to IIS redirection, if you have another module or approach I can use it's very welcome

1

1 Answers

8
votes

The part after the hash sign (officially called the fragment identifier) is a client-side only part of the URL. It's never send to the server. That's why it won't work for rewrite but only for redirects. The rewrite rule will match but IIS will actually try to open a file called hashtest.aspx#hash1 (i.e. a file with the extension .asp#hash1). This file won't be processed as a normal ASP page as the extension is not linked to ASP.NET. And most likely it's content won't even be displayed at all as IIS is by default configured to only allow request for known extensions.