[英]Wordpress Giving Random 404 on Custom Rewrite Rules
我有一个网站针对触发用Wordpress编写的自定义重写的页面给出了间歇性(但很少见)404错误。 漂亮的URL在大多数情况下都会起作用,并且刷新页面会使其正确加载(如果执行404)。
链接已使用以下功能重写:
function my_post_type_link( $link, $post = 0 )
{
$type = $post->post_type;
switch( $type )
{
case 'sfwd-courses': //Change Courses links to Modules
return home_url( 'module/' . $post->ID . '/' . $post->post_name );
case 'sfwd-lessons': //Change Lessons links to Focuses
return home_url( 'focus/' . $post->ID . '/' . $post->post_name );
case 'sfwd-topic': //Change Topics links to Lessons
return home_url( 'lesson/' . $post->ID . '/' . $post->post_name );
case 'my_course':
return home_url( 'course/' . $post->ID . '/' . $post->post_name );
default:
return $link;
}
}
这按预期工作。
然后,我用以下代码重写这些新链接:
function my_rewrites_init()
{
flush_rewrite_rules();
add_rewrite_rule('module/([0-9]+)/([^/]*)?$', 'index.php?post_type=sfwd-courses&p=$matches[1]&name=$matches[2]' , 'top');
add_rewrite_rule('focus/([0-9]+)/([^/]*)?$', 'index.php?post_type=sfwd-lessons&p=$matches[1]&name=$matches[2]' , 'top');
add_rewrite_rule('lesson/([0-9]+)/([^/]*)?$', 'index.php?post_type=sfwd-topic&p=$matches[1]&name=$matches[2]' , 'top');
add_rewrite_rule('course/([0-9]+)/([^/]*)?$', 'index.php?post_type=aha_course&p=$matches[1]&name=$matches[2]' , 'top');
}
add_action( 'init', 'my_rewrites_init');
到目前为止,我所看到的服务器日志仅报告了重写链接上的404,但是访问失败的链接将起作用。
我不确定我是否错过了重写功能中的某些内容,或者问题是否可能出在其他地方。 永久链接已重新创建,但仍然获得随机404。
我仅在本地WAMP开发服务器上触发了404一次,但是生产服务器会更频繁地执行它。 产品服务器是IIS。
谢谢你的帮助。
如果尚未为IIS配置web.config,请添加以下代码:UPDATE:
<rewrite>
<rules><rule name="WordPress Rule" 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="index.php?page_id={R:0}"/></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>
另外,如果您在漂亮的链接(文章标题)中使用了任何非拉丁符号,请将以下代码添加到wp-config.php中:
if ( isset($_SERVER['UNENCODED_URL']) ) {
$_SERVER['REQUEST_URI'] = $_SERVER['UNENCODED_URL'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.