Spotlight Spotlight - 1 month ago 14
PHP Question

IIS php extension in URL

I'm migrating an Apache server to an IIS server.

Currently, in the Apache server, if we have a PHP page named www.example.com/test.php, we can access it even without the final ".php" (i.e. www.example.com/test).

How to achieve a similar behavior in IIS?

Answer

With IIS8 and above Microsoft implemented the Web Platforms and you can download the module URL Rewrite from there (or just download it from their site).

After that, go to the IIS Service Management and click on "URL Rewrite"

URL Rewrite on IIS 8

At the right panel, you'll see a option to "Import rules.."

Import htaccess rules into IIS

Choose your .htaccess (that you currently have on your public_html) and click on the "Import" button.

This creates a file called web.config in the path of your site with a content that will look like the follwing:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect index.php" stopProcessing="true">
                    <match url="index\.php/(.*)" />
                    <action type="Redirect" url="{R:1}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

There are also some online services that may convert your .htaccess to web.config although I've tried a few and the import from the IIS Service Management works better.

Comments