11

I have a simple wildcard routing rule I want to apply for my Azure web app.

<rule name="MyRule">
  <match url="*" />
  <action type="Rewrite" url="/index.html" />
</rule>

Do I have any option here given I can't RDP into the machine and fiddle with IIS? This is not an ASP.Net website, it's a simple SPA application.

3 Answers 3

16

You need to create a web.config file in your wwwroot folder and put the relevant config entries there.

Here's an example of an web.config rule, to give you an idea of what it should look like.

The below example redirect the default *.azurewebsites.net domain to a custom domain (via http://zainrizvi.io/blog/block-default-azure-websites-domain/)

<configuration>
  <system.webServer>  
    <rewrite>  
        <rules>  
          <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^yoursite\.azurewebsites\.net$" />
            </conditions>
            <action type="Redirect" url="http://www.yoursite.com/{R:0}" />  
          </rule>  
        </rules>  
    </rewrite>  
  </system.webServer>  
</configuration>
3
  • Ensure you have installed the IIS Rewrite Module locally too :) iis.net/downloads/microsoft/url-rewrite
    – Matt
    Jul 31, 2018 at 18:23
  • @Zain Rizvi the link you provided is not working anymore
    – Andreas
    Dec 13, 2018 at 13:44
  • the above code look fine but when I edit my web.config file and past rewrite code and save the file and again on refresh pasted code disappeared? any logic, site is connected with pipeline
    – Saad Awan
    Sep 28, 2020 at 12:09
7

If simply want all URL's that resolve to this server & site to redirect to index.html you could use this rewrite section:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA">
                    <match url=".*" />
                    <action type="Rewrite" url="index.html" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This is very similar to what you have except some minor syntax fixes e.g. the pattern should be ".*" and the rewrite URL target simply "index.html". Note this means that ALL URL's to your site will be rewritten, even for other resources like CSS and JS files, images etc. So you'd better be fetching your resources from other domains.

0
5

If you want to do actual rewrites (not redirects), dont forget enabling ARR with applicationHost.xdt file put to the site folder with the following content:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" />
    <rewrite>
      <allowedServerVariables>
        <add name="HTTP_ACCEPT_ENCODING" xdt:Transform="Insert" />
        <add name="HTTP_X_ORIGINAL_HOST" xdt:Transform="Insert" />
      </allowedServerVariables>
    </rewrite>
  </system.webServer>
</configuration> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.