URL rewrites for Angular

september 18, 2016|Notater|

Apache

.htaccess

RewriteEngine On
  # If an existing asset or directory is requested go to it as it is
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]
  # If the requested resource doesn't exist, use index.html
  RewriteRule ^ /index.html

IIS

web.config

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- rewrite to https -->
        <rule name="Redirect to HTTPS" stopProcessing="true">
          <match url="(.*)" negate="false"/>
          <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="^OFF$"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
        </rule>
        <!-- ignore static files -->
        <rule name="Angular Conditions" stopProcessing="true">
          <match url="(assets/.*|b/.*)"/>
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory"/>
          </conditions>
          <action type="None"/>
        </rule>
        <!-- rewrite everything else -->
        <rule name="Angular Wildcard" enabled="true" stopProcessing="true">
          <match url="(.*)"/>
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory"/>
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <!-- set default document -->
    <defaultDocument>
      <files>
        <clear />
        <add value="index.html" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>
Share this Post: