To convert Laravel's .htaccess
rules to a web.config
file for IIS, follow these steps. This conversion includes setting rules for redirecting HTTP to HTTPS (commented out), redirecting from www to non-www (also commented out), handling the authorization header, and redirecting to the public folder or server.php
file if the request is not for an existing directory or file. With this configuration, IIS can emulate the Apache mod_rewrite behavior commonly used in Laravel applications.
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- Redirect HTTP to HTTPS -->
<!--
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
-->
<!-- Redirect www to non-www -->
<!--
<rule name="WWW to non-WWW redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
-->
<!-- Handle Authorization Header -->
<rule name="Authorization Header">
<match url=".*" />
<conditions>
<add input="{HTTP_AUTHORIZATION}" pattern=".+" />
</conditions>
<serverVariables>
<set name="HTTP_AUTHORIZATION" value="{HTTP_AUTHORIZATION}" />
</serverVariables>
<action type="None" />
</rule>
<!-- If the request is for a directory or file, serve it -->
<rule name="StaticContent">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="None" />
</rule>
<!-- Otherwise, pass the request to public folder or server.php -->
<rule name="Rewrite to Public Folder">
<match url="(.*\.\w+)$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="public/{R:1}" />
</rule>
<rule name="Rewrite to Server PHP">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="server.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Top comments (0)