DEV Community

yysfyn
yysfyn

Posted on

2

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! Stream

Go beyond the firewall

There's more to security than code. Explore solutions, strategies, and the full story on AWS Security LIVE!

Learn More

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay