通过Web.config将'.php'扩展名重定向到Azure中WordPress页面上的非扩展名URL

问题描述 投票:0回答:1

Dependencies:我正在Azure应用服务上运行WordPress网站。

目标:如果有人访问:

example.com/my-page.php

然后将它们重定向到:

example.com/my-page/

OR(不带反斜杠):

example.com/my-page

我一直在StackOverflow各处寻找解决方案,但是WordPress / Azure所针对的内容都不存在,因此无法正常工作。

答案很简单,通过Apache和.htaccess文件:

# 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

但是当我尝试使用除WordPress声明为默认重定向之外的任何内容来编辑web.config文件时,事情会变得很棘手。

这是我目前在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>

第一个规则通过WordPress的index.php转发所有请求。第二条规则是我在网上找到的,可以帮助将以.php结尾的所有请求转换为非扩展URL。它不起作用。

有人针对这种特殊情况有可行的解决方案吗?

php wordpress azure url-rewriting web-config
1个回答
0
投票

听起来您有一个适用于Apache的.htaccess文件,因此您可以尝试参考博客Convert .htaccess to web.config来实现您的需求。并且要遵循博客的How to use IIS Manager to import .htaccess files部分,您可以更轻松地使用IIS Manager的工具URL Rewrite > Import Rules > Import mod_rewrite Rules自动进行转换。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.