Search code examples
azureazure-webapps

How to remove .php extension on Azure PHP Web App


How do I remove the .php extension on a Azure PHP Web App ?

using at htaccess file with the following, does NOT work

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Ideas ?


Solution

  • Azure Web Apps use IIS to host your application. The best equivalent of Apache's .htaccess system is actually using web.config file to handle URL rewrite in IIS.

    For example, you can just put the following web.config file into your web app's root folder to hide .php extension.

    Referece:

    https://www.saotn.org/iis-url-rewrite-hide-php-extension.

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="hide .php extension" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}.php" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    Hope it helps.