WordPress add_rewrite_rule works locally but not on server

Viewed 4124

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.

4 Answers
Related