WordPress add_rewrite_rule works locally but not on server

apache, mod-rewrite, regex, wordpress, wordpress-theming

Solution

This was to long to add as a comment.

I would add this to functions.php. I've changed your permalink a bit, my demo plugin explains what each function does:

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);
    }
}

Problem

I'm able to get `add_rewrite_rule` to work locally with MAMP, but not on my DreamHost production server. I'm attempting to prettify the URLs for individual pets at http://www.pawsnewengland.com/our-dogs-list/. The ugly URLs follow this structure: our-dogs-list/?view=pet-details&id=12345, and a custom function uses `$_GET` variables to process in the information. In my `functions.php` file, I've included this: ``` 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'); ``` I've also tried this with the same results: ``` 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'); ``` And in an effort to simply test that rewrites would work at all, tried this: ``` function rewrite_pet_url() { add_rewrite_rule( "fake", "index.php/about", "top" ); } add_action( 'init', 'rewrite_pet_url'); ``` I'm flushing rewrite rules prior to testing, and have confirmed that the rewrite rules were added to the `.htaccess` file. For some reason, though, I'm seeing either a 404 page or a white screen and "No input file specified." I'm able to get this working locally, so I have no idea what's breaking on a live server. Any insights? Update 1 I've gotten the rewrite "working" in the sense that it no longer causes any errors. Unfortunately, it now causes an unwanted redirect to the root 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' ); ``` With this, I can access the `view` and `id` variables using `get_query_var()`. However, instead of honoring `example.com/our-dogs-list/pet-details/12345`, WordPress redirects the page to `example.com/our-dogs-list/`. Any idea what could be causing that? It's effectively making the rewrite rule useless.

Original source