60

I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to example.com/page to example.com/page.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <rewrite>
          <rewriteMaps>
              <rewriteMap name="StaticRedirects">
                  <add key="/page" value="/page.html" />
              </rewriteMap>
            </rewriteMaps>
      </rewrite>
  </system.webServer>
</configuration>

A couple of problems arise:

  1. I don't know where to even put the file. There is a User root directory, and an htdocs directory, I tried both, no joy.
  2. I don't even know if the account can do rewrites, I am trying to find that out.
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
J. Martin
  • 1,683
  • 2
  • 17
  • 33

3 Answers3

126

1) Your existing web.config: you have declared rewrite map .. but have not created any rules that will use it. RewriteMap on its' own does absolutely nothing.

2) Below is how you can do it (it does not utilise rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):

This rule will do SINGLE EXACT rewrite (internal redirect) /page to /page.html. URL in browser will remain unchanged.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SpecificRewrite" stopProcessing="true">
                <match url="^page$" />
                <action type="Rewrite" url="/page.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

This rule #2 will do the same as above, but will do 301 redirect (Permanent Redirect) where URL will change in browser.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SpecificRedirect" stopProcessing="true">
                <match url="^page$" />
                <action type="Redirect" url="/page.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Rule #3 will attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /page it will check if /page.html exists, and if it does then rewrite occurs):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="DynamicRewrite" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
                </conditions>
                <action type="Rewrite" url="/{R:1}.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 3
    Was trying to understand these maps and your answer helped alot. Thanks. My +1 – AYK Nov 07 '12 at 08:49
  • 2
    @CodeSherpa I believe Tomek gave correct solution a bit: just alter yours: add `` and maybe use `^register\.aspx$` (notice the \ character -- this will ensure that only `register.aspx` will be redirected, and not `register2aspx` etc.). Also -- make sure that **URL Rewrite Module** is installed (it is separate download = not bundled with IIS by default). – LazyOne Dec 17 '12 at 23:15
  • 1
    @LazyOne can u help me out for http://stackoverflow.com/questions/19904849/url-mapping-works-on-localhost-but-not-on-production-server-in-asp-net – SHEKHAR SHETE Nov 11 '13 at 11:27
  • @SHEKHARSHETE That's different: It's URL Rewriting module here while yours is actual .NET config (which works a bit differently, AFAIK, and has nuances, which I'm not familiar with). – LazyOne Nov 11 '13 at 11:48
  • do u have any complete sample example for above so that i should try? i am new to the url rewritting module so...! – SHEKHAR SHETE Nov 11 '13 at 11:53
  • i am trying url rewritting with Query string but no get success plz help me ` ` and web config rul is ` ` how to show url linke EditClient.aspx/0 or something numeric id plz help me my Ac block so ask Que – jayesh dhameliya Apr 14 '16 at 11:31
18

Just wanted to point out one thing missing in LazyOne's answer (I would have just commented under the answer but don't have enough rep)

In rule #2 for permanent redirect there is thing missing:

redirectType="Permanent"

So rule #2 should look like this:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SpecificRedirect" stopProcessing="true">
                <match url="^page$" />
                <action type="Redirect" url="/page.html" redirectType="Permanent" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Edit

For more information on how to use the URL Rewrite Module see this excellent documentation: URL Rewrite Module Configuration Reference

In response to @kneidels question from the comments; To match the url: topic.php?id=39 something like the following could be used:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="SpecificRedirect" stopProcessing="true">
        <match url="^topic.php$" />
        <conditions logicalGrouping="MatchAll">
          <add input="{QUERY_STRING}" pattern="(?:id)=(\d{2})" />
        </conditions>
        <action type="Redirect" url="/newpage/{C:1}" appendQueryString="false" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

This will match topic.php?id=ab where a is any number between 0-9 and b is also any number between 0-9. It will then redirect to /newpage/xy where xy comes from the original url. I have not tested this but it should work.

Hjalti
  • 307
  • 2
  • 12
  • Hey. This solution works great for statis files. How would it look with a match url of: `match url="topic.php?id=39"` ? – kneidels May 04 '15 at 11:30
  • 1
    @kneidels: I just updated the answer, see if that helps – Hjalti May 05 '15 at 15:10
  • +1 for providing the link to the iis.net documentation ( which I see is redirected now to https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference ) – fortboise Aug 30 '18 at 17:57
  • So I managed to get this to work but for some reason it only work if have the directory ~ i.e. /pages - why is this directory needed ~ can I just reference it without it needing to exist? – user7201898 Aug 26 '22 at 16:04
12

Just tried this rule, and it worked with GoDaddy hosting since they've already have the Microsoft URL Rewriting module installed for every IIS 7 account.

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
      <match url="^enquiry$" />
      <action type="Rewrite" url="/Enquiry.aspx" />
    </rule>
  </rules>
</rewrite>
p.campbell
  • 98,673
  • 67
  • 256
  • 322
nolimit
  • 814
  • 10
  • 19
  • 3
    Thanks to you mentioning the module being installed I realised that I didn't have it installed and managed to overcome my problem :) – lappy Oct 04 '14 at 08:36