This may seem like a duplicate question, but I've read through as many SO posts with similar titles, and every one of them got much farther than I've been able to, so it seems that I must be doing something basic wrong, but I have no idea what it is. That said, here's my issue:
I'm following the steps in the 'Basic Usage' section of the add_rewrite_rule documentation page, making sure to flush and regenerate rewrite rules.
I've got a page with an id of 68, and I want to redirect this:
http://localhost.test.wordpress/leaf/68
to this:
http://localhost.test.wordpress/index.php?page_id=68
I have verified that the latter does exist, and that I can navigate to it directly.
So in my theme's functions.php
file, I wrote the following:
function custom_rewrite_basic() {
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
And then I went to the Settings -> Permalinks section of my WP admin panel and hit 'Save Changes' with no changes, as the docs page directs me to do.
The problem I'm running into is that when I navigate to http://localhost.test.wordpress/leaf/68
, I get a 404 error. Clearly, the docs indicate that this should not be happening, and none of the other SO posts I've seen have had this problem (they get past this step and run into difficulty in some other aspect of the redirect).
The redirect code I put in functions.php
is an exact copy/paste from the docs page, and yet because of the 404, I'm led to believe that no WP code is being run at all. Can you let me know what I'm doing wrong? I'm completely new to WP.
Based on your supplemental answers in the comments, your .htaccess doesn't sound like it's setup properly (getting server 404 page instead of WP 404). Add the following to your .htaccess file:
This redirects (almost) every request to index.php, to be processed by WP internally. This needs to be set up properly for your rewrite rules to be processed by WP.
If the site doesn't seem to be respecting changes you make to the .htaccess file, you should make sure that the
<VirtualHost>
configuration explicitly gives the .htaccess files permission to override certain configurations.In your
<VirtualHost>
entry there should be a<Directory>
entry as well. You'll need to add theAllowOverride All
command to it, like this:Also, if the site isn't respecting your
Rewrite
commands, you should make sure to check yourhttpd.conf
to see if the mod_rewrite module is loaded. Look for a line similar to this:If it is commented out with a #, uncomment that line (remove the #) and then restart your apache.