add_rewrite_rule() driving me crazy, rewrite not working when analyzer says it should
I have a special situation and need some help achieving the final step.
Background:
When I access mydomain.com/category/postname, I get the post as expected.
When I access mydomain.com/category/postname?ajax=1, I get the post html (baked into and including its corresponding page template markup, which is extremely important for this use case), but I get it rendered without its header and footer (I did this on purpose, with a $_GET['ajax']==1 conditional inside the header.php and footer.php files to suppress them).
This all works as expected. However, it leaves me with a new small problem... Browsers won't properly cache content from URLs that end with a GET parameter string.
Therefore, I would like to create a rewrite rule that achieves the following...
mydomain.com/ajax/category/postname becomes mydomain.com/category/postname?ajax=1
Here's from my functions.php:
function dynaload_rewrite() {
add_rewrite_rule('ajax/(.*)/?', '$matches[1]?ajax=1', 'bottom');
}
add_action('init', 'dynaload_rewrite');
This results in .htaccess file as follows:
# BEGIN WordPress
IfModule mod_rewrite.c
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^ajax/(.*)/? /$matches[1]?ajax=1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule
# END WordPress
Expected Result: When I access /ajax/category/postname I want the content from /category/postname to render, minus the header and footer, as they do if I access normally with ?ajax=1
Actual Result: I am receiving the content from my WordPress 404 page, however the header and footer are omitted (so I know that the parameter ajax=1 is being passed correctly, it's just loading the wrong page).
Successful help is appreciated and will be deemed to be impressive. Thank you.
Topic rewrite-rules mod-rewrite Wordpress
Category Web