Search code examples
web-configurl-rewritingiis-7.5windows-server

IIS web.config enable rewrite rules


I am trying to remove the extension .php from the url, so that mysite.com/home.php can work as mysite.com/home.

I am hosting my site on 1and1.com and I asked them if the Rewrite engine was turned on, and they said that for the windows server running IIS 7.5 that it is not turned on and that I could turn it on with some code in the web.config file.

I have not been able to find code that turns the rewrite rules on.

Below is the what I tried to use to rewrite urls. But I get an Error 500.19.

Is there really a way to turn rewriting on in the web.config file?

web.config file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>    
    <system.webServer>
        <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
             <add value="home.php" />
             <add value="index.html" />
            </files>
        </defaultDocument>
        <rewrite>
            <rules>
                <rule name="rewrite php">
                  <!--Removes the .php extension for all pages.-->
                  <match url="(.*)" />
                  <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
                  </conditions>
                  <action type="Rewrite" url="{R:1}.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>  
</configuration>

Solution

  • Found out that 1and1.com had the rewrite engine turned off for the web.config file.

    But I found out that I can use the web.config file to redirect to a homepage while using the .htaccess file and it's rewrite rules to remove the .php extension from the url.

    web.config file

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>    
        <system.webServer>
            <directoryBrowse enabled="false" />
            <defaultDocument>
                <files>
                    <clear />
                 <add value="home.php" />
                 <add value="index.html" />
                </files>
            </defaultDocument>
    
        </system.webServer>  
    </configuration>
    

    .htaccess file

    AddHandler php5-script .php
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !\.php$
    RewriteRule ^(.*)$ $1.php [L]
    

    Both file are located in the root. And works perfectly.