Chr*_*ndi 7 regex apache wordpress mod-rewrite wordpress-theming
我可以add_rewrite_rule在MAMP本地工作,但不能在我的DreamHost生产服务器上工作.
我试图在http://www.pawsnewengland.com/our-dogs-list/上美化个别宠物的网址.丑陋的URL遵循以下结构:our-dogs-list /?view = pet-details&id = 12345,自定义函数使用$_GET变量处理信息.
在我的functions.php文件中,我已经包含了这个:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php/our-dogs-list/?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
Run Code Online (Sandbox Code Playgroud)
我也尝试了同样的结果:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php/our-dogs-list/?view=pet-details&id=$matches[1]",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
Run Code Online (Sandbox Code Playgroud)
为了简单地测试重写是否会起作用,试过这个:
function rewrite_pet_url() {
add_rewrite_rule(
"fake",
"index.php/about",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
Run Code Online (Sandbox Code Playgroud)
我在测试之前刷新重写规则,并确认重写规则已添加到.htaccess文件中.但是,出于某种原因,我看到的是404页面或白色屏幕并且"未指定输入文件".
我能够在本地工作,所以我不知道在现场服务器上有什么打破.任何见解?
我已经将重写"工作",因为它不再导致任何错误.不幸的是,它现在导致不必要的重定向到根URL.
function rewrite_pet_url() {
add_rewrite_tag('%view%','([^&]+)');
add_rewrite_tag('%id%','([^&]+)');
add_rewrite_rule(
'our-dogs-list/pet-details/([0-9]+)?$',
'index.php/?page_id=1663&view=pet-details&id=$1',
'top'
);
}
add_action( 'init', 'rewrite_pet_url' );
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以使用view和访问id变量get_query_var().但是,example.com/our-dogs-list/pet-details/12345WordPress 不是尊重,而是将页面重定向到example.com/our-dogs-list/.
知道是什么原因造成的吗?它有效地使重写规则变得毫无用处.
这需要很长时间才能添加评论.
我会将它添加到functions.php中.我已经改变了你的永久链接,我的demo插件解释了每个函数的作用:
add_filter('generate_rewrite_rules', 'pet_rewrite_url');
add_filter('query_vars', 'pet_query_vars'));
add_filter('admin_init', 'pet_flush_rewrite_rules');
add_action("parse_request", "pet_parse_request");
function pet_rewrite_url( $wp_rewrite ) {
$new_rules = array(
'our-dogs-list/pet-details/([0-9]+)/?$' => sprintf("index.php?pet-details=%s",$wp_rewrite->preg_index(1))
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
function pet_query_vars( $query_vars ) {
$query_vars[] = 'pet-details';
return $query_vars;
}
function pet_flush_rewrite_rules() {
$rules = $GLOBALS['wp_rewrite']->wp_rewrite_rules();
if ( ! isset( $rules['our-dogs-list/pet-details/([0-9]+)/?$'] ) ) { // must be the same rule as in pet_rewrite_url($wp_rewrite)
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
function pet_parse_request($wp_query) {
if (isset($wp_query->query_vars['pet-details'])) { // same as the first custom variable in pet_query_vars( $query_vars )
// add your code here, code below is for this demo
printf("<pre>%s</pre>",print_r($wp_query->query_vars,true));
exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
您的替换看起来不正确,请尝试:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
Run Code Online (Sandbox Code Playgroud)
请注意使用index.phpand notindex.php/our-dogs-list/作为目的地。
或者也许是这样的:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"our-dogs-list/index.php?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2574 次 |
| 最近记录: |