[英]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.