I can get add_rewrite_rule to work locally with MAMP, but not on my DreamHost production server.
I am trying to clear the URLs for individual pets http://www.pawsnewengland.com/our-dogs-list/ . Ugly URLs follow this structure: our-dogs-list /? View = pet-details & id = 12345, and the user-defined function uses the $_GET variables to process the information.
In my functions.php file, I 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 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, trying to just verify that the rewrites will work at all, try this:
function rewrite_pet_url() { add_rewrite_rule( "fake", "index.php/about", "top" ); } add_action( 'init', 'rewrite_pet_url');
I am trying to rewrite the rules before testing and confirmed that the rewrite rules have been added to the .htaccess file. For some reason, however, I see either a 404 page or a white screen, and "There is no specified input file."
I can make this work locally, so I have no idea what is breaking on the real server. Any ideas?
Update 1
I rewrote the βworkβ in the sense that it no longer causes any errors. Unfortunately, now it causes unwanted redirects 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 executing example.com/our-dogs-list/pet-details/12345 WordPress redirects the page to example.com/our-dogs-list/ .
Any idea what could be causing this? This effectively makes the rewrite rule useless.