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.
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:
I’ve written a guide on how to add custom permalinks, here’s the example plugin (also available at my site):
Your replacements look incorrect, try:
Note the use of
index.php
and notindex.php/our-dogs-list/
as the destination.Or maybe it’s like this:
edit in your .htaccess
RewriteRule ^our-dogs-list/pet-details/([0-9]+)?$ /index.php?page_id=1663&view=pet-details&id=$1 [L]
add this forRewriteRule ^index.php$ - [L]
so you .hatcces would something like:update
from: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
so far i understand you should use: add_rewrite_tag() for
view
andid
in your first example.