三十、Wordpress的自定义伪静态规则
Wordpress的伪静态规则存放在wp_options数据表的rewrite_rules的对应值里。
试图将www.xxx.com/url?v=123伪静态成www.xxx.com/url-123.html
functions.php
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// 如果伪静态规则里面没有我们的规则,则进行重置
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['url-(.*).html$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
//添加一个新的伪静态规则
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['url-(.*).html$'] = 'index.php?pagename=url&v=$matches[1]';
return $newrules + $rules;
}
//添加一个变量名称
function my_insert_query_vars( $vars )
{
array_push( $vars, 'v' );
return $vars;
}