edit-tags.php 返回404错误 IIS windows 系统

技术文章2年前 (2023)发布 gyx131
289 0 0

网站用的是 web.config 文件(一种 IIS 的配置文件)来进行网站配置。

web.config 文件里面会写入伪静态规则的配置:

<?xml version="1.0" ?>
<configuration>
	<location allowOverride="false" inheritInChildApplications="false" path=".">
		<system.webServer>
			<rewrite>
				<rules configSource="web_config\rewrite.config"></rules>
			</rewrite>
			<defaultDocument configSource="web_config\default.config"></defaultDocument>
			<httpErrors configSource="web_config\httpErrors.config"></httpErrors>
			<handlers configSource="web_config\php.config"></handlers>
		</system.webServer>
	</location>
</configuration>

可以发现,这里的这一行:<rules configSource="web_config\rewrite.config"></rules> 表示的就是在下一级目录中有一个叫做 rewrite.config 的文件是用来写伪静态的规则的。

目前宝塔面板给的 wordpress 默认的伪静态规则是这样的:

<?xml version="1.0" ?>
<rules>
	<rule name="category_rewrite">
		<match url="category/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action appendQueryString="false" logRewrittenUrl="false" type="Rewrite" url="/index.php?category_name={R:1}"/>
	</rule>
	<rule name="tags_rewrite">
		<match url="tag/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action type="Rewrite" url="index.php?tag={R:1}"/>
	</rule>
	<rule name="Main Rule_rewrite" 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_rewrite" 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>

其中大家可以看到有四段规则。当然这些我百度了老半天了,一直没能解决问题,也看了一些英文网站,依然无法解决。当然在宝塔的论坛也有发过帖子,看着别人的问题都是有解答的我想这个用你们产品的问题应该有解答吧,应该很快吧。但让我失望的是,几个月过去了,宝塔的运维理都不理,还好近期看到自己的帖子被另外一位网友评论了,说自己也遇到了类似的问题,但不知道怎么解决。

可以说,这个办法是全网首发。

我只好自己找资料,自己尝试写规则测试。还好,我看得懂一些英文。雅思英语还是过关的。

最后我想了一个办法,将这里的四段规则删除一些,留下一些,把可疑的单独删除,看能否正常访问。幸运的是,最后让我测试出来了,问题出现在这一段上:

        <rule name="tags_rewrite">
		<match url="tag/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action type="Rewrite" url="index.php?tag={R:1}"/>
	</rule>

我一想,这是用来给 tag 前台标签能正常访问的伪静态规则,为啥会影响到后台页面呢?后来我猜,有可能是我访问的分类目录和标签的页面都有 edit-tags.php,这不有 tag 单词在里面吗?难道是多匹配了?

这里有两个解决方案,目前还未确定哪种最好,我需要先用一段时间再看吧。

第一种方案:

直接把这段规则删除。

我删除后,无需重启服务器等,分类目录、标签等页面就已经能正常访问了,也能给文章加标签了。

但我猜测这样有可能前台访问类似这样的链接:

http://fox-9.com/tag/post/

的时候会找不到网页。

但让我诧异的是,访问也是正常的。

目前我用这种方法测试一下后续是否有问题。

所以直接删除造成的问题我们后面再看。

补充说明一点,我是安装了一个中文 tag 也能显示的插件的,叫做:IIS Chinese Tag Permalink

这个插件就一个 iis-chinese-tag-permalink.php 文件,它的代码为:

<?php
/*
Plugin Name: IIS Chinese Tag Permalink
Plugin URI: http://blog.bossma.cn/php/in-iis-wordpress-chinese-tag-permalinks-plugin/
Description: 使IIS下的WordPress可以在友好的固定链接(Pretty Permalink)中使用中文。
Author: BOSSMA.CN
Author URI: http://blog.bossma.cn/
Version: 1.4
*/
add_action('init', 'bossma_get_urf8_pathandurl');
add_filter('get_pagenum_link','bossma_set_utf8_pagenumlink');
add_action('parse_query','bossma_chinese_tag_encode');

// iis encode the url with GBK,but wordpress with utf-8
function bossma_get_urf8_pathandurl() {
	$_SERVER['PATH_INFO']=iconv("GBK","UTF-8",$_SERVER['PATH_INFO']);
	$_SERVER['REQUEST_URI']=iconv("GBK","UTF-8",$_SERVER['REQUEST_URI']);
}

// urlencode for chinese tag
// sometimes preg_match can not match the chinese correctly 
function bossma_chinese_tag_encode($query){
	if(isset($is_tag)&&$is_tag){
		if($query->query_vars['tag']!=''){
			$query->query_vars['tag']=urlencode($query->query_vars['tag']);
		}
	}
}

//for generate tag pagenum link
//use utf8 encode
function bossma_set_utf8_pagenumlink($result){
 $tag_index=stripos($result,"/tag/");
 $link_len=strlen($result);
 if($tag_index!==false){
  $link_base=substr($result,0,$tag_index);
  $page_index=stripos($result,"/page/");
  if($page_index!==false){
   $tag_str=substr($result,$tag_index+5,$page_index-$tag_index-5);
   $result=$link_base."/tag/".rawurlencode($tag_str).substr($result,$page_index);
  }else{
   $tag_str=substr($result,$tag_index+5);
   $result=$link_base."/tag/".rawurlencode($tag_str)."/";
  }
 }
 return $result;
}
?>

第二种方案:

将伪静态规则修改为这样,让它匹配不到 edit-tags

        <rule name="tags_rewrite">
		<match url="/tag/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action type="Rewrite" url="index.php?tag={R:1}"/>
	</rule>

可以看到就多加了一个 /,匹配规则变成了 /tag/?(.*),这样就不会匹配到 edit-tags 路径了。

目前测试发现,这个方法存在一个问题,就是偶尔的,文章在添加新的标签的时候,或者在所有文章的页面点击 “快速编辑” 按钮的时候会无法编辑或者刷新页面。

如果你也有类似的问题,希望我的文章和解决方案能够帮到你,谢谢!欢迎跟我交流!

© 版权声明

相关文章

暂无评论

暂无评论...