正则表达式 – WordPress add_rewrite_rule在本地工作,但不在服务器上

前端之家收集整理的这篇文章主要介绍了正则表达式 – WordPress add_rewrite_rule在本地工作,但不在服务器上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我能够让add_rewrite_rule在本地与MAMP一起工作,但不能在我的DreamHost生产服务器上工作.

我试图在http://www.pawsnewengland.com/our-dogs-list/处对个别宠物的URL进行美化.丑陋的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');

我也尝试了同样的结果:

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]",'rewrite_pet_url');

为了简单地测试重写是否会起作用,试过这个:

function rewrite_pet_url() {
    add_rewrite_rule(
        "fake","index.PHP/about",'rewrite_pet_url');

我在测试之前刷新重写规则,并确认重写规则已添加到.htaccess文件中.但是,出于某种原因,我看到的是404页面或白色屏幕并且“未指定输入文件”.

我能够在本地工作,所以我不知道在现场服务器上有什么打破.任何见解?

更新1

我已经将重写“工作”,因为它不再导致任何错误.不幸的是,它现在导致不必要的重定向到根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' );

有了这个,我可以使用get_query_var()访问视图和id变量.但是,wordpress不是尊重example.com/our-dogs-list/pet-details/12345,而是将页面重定向到example.com/our-dogs-list/.

知道是什么原因造成的吗?它有效地使重写规则变得毫无用处.

这需要很长时间才能添加评论.

我会将它添加到functions.PHP中.我已经改变了你的固定链接,我的demo plugin解释了每个功能的作用:

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);
    }
}
原文链接:https://www.f2er.com/regex/356590.html

猜你在找的正则表达式相关文章