Redirect '.php' extension to non-extension url on WordPress page in Azure through web.config

Dependencies : I'm running a WordPress site on an Azure App Service.

The Goal : If anyone visits :

example.com/my-page.php

They are then redirected to:

example.com/my-page/

OR (without the backslash):

example.com/my-page

I've looked all over StackOverflow to find a solution, but nothing has been WordPress/Azure specific, and hence didn't work.

The answer is simple via Apache and an .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

But when I try to edit the web.config file with anything other than what WordPress declares as the default redirect, things get tricky.

Here's what I currently have in my web.config :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="WordPress: https://example.com" patternSyntax="Wildcard">
          <match url="*"/>
            <conditions>
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
            </conditions>
          <action type="Rewrite" url="index.php"/>
        </rule>
        <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>

The first rule forwards all requests through index.php for WordPress. The second rule was something I found online to help convert all requests ending in .php to non-extension URLs. It doesn't work.

Does anyone have a working solution for this particular situation?

Solutions

Got it. Here's my solution:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Remove any php extensions if it ends in php">
          <match url="^(.*)\.php$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Redirect" url="{R:1}" />
        </rule>
        <rule name="Wordpress" patternSyntax="Wildcard">
          <match url="*"/>
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
          </conditions>
          <action type="Rewrite" url="index.php"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

It sounds like you have a worked .htaccess file for Apache, so that you can try to refer to the blog Convert .htaccess to web.config to realize your needs. And to follow the section How to use IIS Manager to import . htaccess files of the blog, you can easier to use the tool URL Rewrite > Import Rules > Import mod_rewrite Rules of IIS Manager to convert it automatically.

Hope it helps.

Similar questions

Extraction of connection string from web config using web service
I have this tricky scenario and want so advice to you. My client have a website built in WordPress and a web application that was built in .Net 3.5 now they want me to integrate this too site by querying some information from the database that .Net is using and display the output on the said website. Communication between the two site is complete a...
Wordpress site on azure web app redirect to 404 error page
I've a wordpress based internet site hosted within an Azure web app. In case I enter a various string behind the "/" of my URL (for instance: "www.blabla.net/asdfasdf") I get redirected to my configured 404 Error page. BUT: In case the URL string contains one or multiple "+" characters (for instance: "www.blabla.net/as+asdf" or just "www.blabla.net...
Moving existing WordPress site on trial Azure to another Azure account
I currently have a WordPress site on a trial account on Azure. The trial is running out in a week so I need to migrate it to another existing Azure account. I was hoping to get some help on how to move the entire site. I exported a WPRESS File + XML Doc under WordPress dashboard and created a backup storage account (on trial Azure). Would it be jus...
Configure Wordpress on Azure Cloud Service to connect to Azure MySQL over SSL
We run Wordpress in a sub folder of our main .NET solution on a cloud service. We have moved our MySQL from CloudDB to Azure MySQL, however it will only connect if we set the "Enforce SSL Connection" to disabled. The Wordpress wp-config.php has the following I presume the issue is we need to pass a certificate, but it is not clear to me how we can ...
Moving wordpress website with data changed from Azure account to another account on Azure
I have installed wordpress website on my Azure account then I imported all the data in additional that I made a lot of changes in the theme. Now, I want to move this wordpress website from this account to another account on Azure and I already downloaded the theme in additional to plugins then I uploaded to this account but it seems that I make all...
Is there an equivalent to config transforms that works with PHP/WordPress on Microsoft Azure?
I've been experimenting with using WebMatrix and Web Platform Installer to spin up simple WordPress sites. It's quite impressive - the out-of-the-box experience is really good, and publishing directly from within WebMatrix works well. I'm now looking at deploying to Azure from GitHub, instead of relying on WebMatrix. The deployment hook is working ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .