我希望我的WordPress网站的每个URL/fr/something
显示与完全相同的页面/something
, 但是without redirecting; 我想保留URL。
我想我很接近,我用过:
add_filter( \'rewrite_rules_array\', \'my_insert_rewrite_rules\' );
add_filter( \'query_vars\', \'my_insert_query_vars\' );
add_action( \'init\', \'my_flush_rules\' );
// flush_rules() if our rules are not yet included
function my_flush_rules() {
$rules = get_option( \'rewrite_rules\' );
if ( ! isset( $rules[\'fr(\\/(.*))?$\'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules[\'fr(\\/(.*))?$\'] = \'index.php?pagename=$matches[1]\';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars ) {
array_push( $vars, \'id\' );
return $vars;
}
它适用于某些URL,但并非所有URL:
/fr
显示与相同/
=> 精细/fr/page-d-example
显示与相同/page-d-example
=> 精细/fr/2015/09/25/bonjour-tout-le-monde/
重定向到/2015/09/25/bonjour-tout-le-monde/
=> 不好
您知道为什么最后一个会重定向,以及如何防止吗?
SO网友:Dave Romsey
米洛的建议为曼苏尔·汗指明了正确的方向:
在您的rewrite_rules_array
可以查看的筛选器$rules
并查看需要复制的所有内容。它们有很多——单篇文章、作者档案、日期档案、分类法档案、文章格式、附件,以及所有这些内容的分页和提要。
更新后的工作代码:
add_filter( \'rewrite_rules_array\', \'my_insert_rewrite_rules\' );
add_filter( \'query_vars\', \'my_insert_query_vars\' );
add_action( \'init\', \'my_flush_rules\' );
// flush_rules() if our rules are not yet included
function my_flush_rules() {
$rules = get_option( \'rewrite_rules\' );
if ( ! isset( $rules[\'fr(\\/(.*))?$\'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
foreach( $rules as $k => $r ) {
$newrules[\'fr/\'.$k] = $r;
}
$newrules[\'fr/?\'] = \'/index.php\';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars ) {
array_push( $vars, \'id\' );
return $vars;
}