I have this function in my functions.php of a localhost build:-
function custom_rewrite() {
add_rewrite_rule('.*', 'http://example.com/', 'top');
}
add_action('init', 'custom_rewrite');
As far as I understand, this should redirect any page to example.com. But it's not working. The page loads as normal with no redirect. I've tried flushing after adding the rule, but no joy.
Obviously, I don't actually want to redirect all pages like this, but I'm trying to get a basic rewrite working before fine-tuning it to my needs. But I can't even get over this first hurdle!
I've followed countless tutorials online and can't get any of them to work. Is there a prerequisite for using add_rewrite_rule? And if not, anybody have any idea why this won't work? Is it because I'm in a localhost environment?
Any advice would be greatly appreciated!
EDIT
I followed Jack's advice and changed my rewrite rule to:-
add_rewrite_rule('.*', 'index.php?page_id=81', 'top');
But I'm still not having any luck. I also tried changing the .* catch all to an actual existing pagename like so:-
add_rewrite_rule('^testpage/?', 'index.php?page_id=81', 'top');
And still no redirect.
The page with ID 81 exists as does 'testpage'. Im stumped. Should rewrite rules work on a localhost build? Any further illumination anybody can offer?
index.php
. Have you checked the official codex page for this function? It's pretty straight forward./testpage/
should loadpage_id
81
, but the URL will stay/testpage/
. But I triedadd_rewrite_rule('^testpage/?', 'index.php?page_id=501', 'top');
(where 501 is an ID that exists on my install) and it worked fine, loading that page's content and template. Make sure you're flushing rewrite rules though. It won't work unless you do.