php - Wordpress Giving Random 404 on Custom Rewrite Rules -
i've got site giving intermittent (but rare) 404 errors pages trigger custom rewrite written in wordpress. pretty url work of time , refresh of page make load correctly if 404.
links have been rewritten using following function:
function my_post_type_link( $link, $post = 0 ) { $type = $post->post_type; switch( $type ) { case 'sfwd-courses': //change courses links modules return home_url( 'module/' . $post->id . '/' . $post->post_name ); case 'sfwd-lessons': //change lessons links focuses return home_url( 'focus/' . $post->id . '/' . $post->post_name ); case 'sfwd-topic': //change topics links 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; } }
this works expected.
i rewrite these new links with:
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');
server logs have seen far report 404 on rewritten link visiting failed link work.
i'm not sure if have missed in rewrite function or if problem elsewhere. permalinks have been recreated still getting random 404.
i have triggered 404 once on local wamp dev server production server doing more often. product server iis.
thanks help.
if did not yet configured web.config iis, adding code: 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>
also, if using non-latin symbols in pretty links (article titles), add wp-config.php code:
if ( isset($_server['unencoded_url']) ) { $_server['request_uri'] = $_server['unencoded_url']; }
Comments
Post a Comment