Monday, 20 May, 2024

Configuring Laravel Routing on Windows IIS

Routing URLs when hosting Laravel on a Windows IIS Web server

Preamble

I do a lot of development work with PHP and Laravel. Occasionally the sites are hosted on Windows Internet Information Server (IIS). Since it's an edge case, as most Laravel website are deployed to a LAMP stack, I want explain how to set-up routing on IIS and how to fix 404 unexpected pages when you first deploy.

IIS Configuration

IIS is configured using web.config files. They're very similar to .htaccess files in Apache. If you're lucky you'll have a set-up allowing you to use IIS Manager to configure your web server, and it's higher level server settings, using a nice GUI interface, but it's more usual for web developers to update web.config in an editor. If you've done some ASP.NET website programming you'll be familiar with the process.

URL Rewrite Module

You'll need the URL Rewrite installed to get Laravel to work on IIS. The URL Rewrite Module is an add-on which enables URLs to be rewritten, just like Apache's mod-rewrite extension. PHP frameworks usually come with an example .htacesss file, with all the mod-rewrites rules supplied, to enable correct URL routing, but it's less usual for them to provide a sample web.config file with rewrite rules for IIS.

You'll need the URL Rewrite installed to get Laravel to work on IIS.

Windows IIS usually comes with the Mod Rewrite installed, but if yours doesn't you can download it from https://www.microsoft.com/en-us/download/details.aspx?id=47337. Unfortunately you'll need privileged access install it, so unless you're managing the server yourself you'll need to ask your host to install it for you.

Configuring Laravel's Routing

A barebones Web.config file for Laravel is shown below.

You'll need to upload this to the /public folder of your Laravel application as web.config. If you already have a web.config with other settings then copy the section inside and use that.

?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>

    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>

    </system.webServer>
</configuration>

Summary

There's more to setting up Laravel on IIS than just uploading and configuring a web.config file but this step is the least obvious and easiest to forget.

Want to Thank Me?

Did you like the article? Was it helpful? If so why not buy me a coffee using Paypal? Buy me a coffee at https://www.paypal.me/justaguycoding