简体   繁体   中英

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?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM