【问题标题】:301 path redirect in web.configweb.config 中的 301 路径重定向
【发布时间】:2019-04-16 08:44:36
【问题描述】:

我正在尝试让网络重定向为我的 wordpress 网站工作,但重定向 url 没有任何反应。

该网站是 wordpress 并且有一个现有的 web.config 文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
            <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>
    <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
     </staticContent>
  </system.webServer>
</configuration>

我希望 url 重定向 FROM /path/to/old TO /path/to/new 并在第一条规则之后添加以下内容:

<rule name="Redirect" stopProcessing="true">
  <match url="^path/to/new" />
  <action type="Redirect" url="http:/www.site.com/path/to/new" redirectType="Found"/>
</rule>

所以完整的代码如下所示:

   <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
                <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>
               <rule name="Redirect" stopProcessing="true">
                 <match url="^path/to/new" />
                 <action type="Redirect" url="http:/www.site.com/path/to/new" redirectType="Found"/>
               </rule>
        </rules>
        </rewrite>
        <staticContent>
                <mimeMap fileExtension=".json" mimeType="application/json" />
         </staticContent>
      </system.webServer>
    </configuration>

我是 web.config 的新手,但之前使用过 .htaccess。这里有点不知道为什么它不起作用 - 当我输入应该重定向的 URL 时,网站上什么也没有发生。

【问题讨论】:

    标签: xml apache azure url web-config


    【解决方案1】:

    据我所知,规则按顺序排列优先级,这意味着您的通配符规则在特定规则之前处理所有请求,然后请求与模式不匹配。

    尝试将 Redirect 规则放在 WordPress 规则之前并删除 stopProcessing 属性。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      根据 IIS 文档 https://docs.microsoft.com/en-us/iis/configuration/system.webserver/httpredirect/

      看起来这可以像放一样简单

       <httpRedirect enabled="true" destination="http:/www.site.com/path/to/new" />
      

      &lt;system.webServer&gt; 的根可能甚至在重写模块之前。你能试一试,让我们知道这是否适合你。

      【讨论】:

      • 请注意,您可能需要在服务器的 IIS 上安装 HTTP 重定向模块,如果尚未安装,请检查提供的链接上的设置部分。
      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2017-05-31
      • 2020-03-05
      相关资源
      最近更新 更多