2018-05-18  544 views 评论

WordPress分类伪静态 404异常处理

 标签:        

服务器环境是IIS7.5

一开始是通过Web.config方式写伪静态规则的,如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="category">
          <match url="category/?(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="Rewrite" url="/index.php?category_name={R:1}" appendQueryString="false" logRewrittenUrl="false" />
        </rule>
        <rule name="tags">
          <match url="^tag/?(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="Rewrite" url="index.php?tag={R:1}"  appendQueryString="false" logRewrittenUrl="false" />
        </rule>
        <rule name="Main Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php/{R:0}" />
        </rule>
        <rule name="wordpress" patternSyntax="Wildcard">
          <match url="*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <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>

然后用了上面的分类下的分页始终无法实现,报出404错误页(即http://your host/index.php/category_name=***&paged=**  请求成 http://your host/category/categoryname/page/***)

随后在规则中加了分类分页的规则如下:

<rule name="category paged">
    <match url="^category/?(.*)/page/?(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Rewrite" url="index.php?category_name={R:1}&paged={R:2}"  appendQueryString="false" logRewrittenUrl="false" />
</rule>

然而这回却报出了500错误,崩溃ing,查询了好久的度娘个谷歌都没找到解决方案,很多都说需要改function.php,但是我始终认为这是伪静态规则的事

最终找到的解决方案是:IIS导入Apache  .htaccess规则

方法如下:

1、新建.htaccess 规则 内容如下:

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

2、打开IIS,找到Url重写配置按钮,如果没有的话需要安装Url Rewrite

随后单击右上角操作面板的应用,即导入OK了。

3、到网站根目录下创建Web.config,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="已导入的规则 1" stopProcessing="true">
                    <match url="^index\.php$" ignoreCase="false" />
                    <action type="None" />
                </rule>
                <rule name="已导入的规则 2" stopProcessing="true">
                    <match url="." ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

随后重启网站即可。 其中rule的Name就是导入规则时的名称,见图2.

给我留言

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: