DEV Community

yysfyn
yysfyn

Posted on

1

Laravel: Convert .htaccess File to IIS web.config Configuration

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>
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay