I have this function in my functions.php of a localhost build:-
function custom_rewrite() {
add_rewrite_rule('.*', '/', 'top');
}
add_action('init', 'custom_rewrite');
As far as I understand, this should redirect any page to example. 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?
I have this function in my functions.php of a localhost build:-
function custom_rewrite() {
add_rewrite_rule('.*', 'http://example/', 'top');
}
add_action('init', 'custom_rewrite');
As far as I understand, this should redirect any page to example. 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?
Expanding on my comment:
The Rewriting API is for rewriting URLs, not redirecting. So the URL won't change. You're just telling WordPress that /testpage/
should load page_id
81
, but the URL will stay /testpage/
.
Regardless, I tried the below, (where 501 is an ID that exists on my install) and it worked fine, loading that page's content and template:
function wpse_283104_rewrite() {
add_rewrite_rule('^testpage/?', 'index.php?page_id=501', 'top');
}
add_action( 'init', 'wpse_283104_rewrite' );
Make sure you're flushing rewrite rules though. It won't work unless you do.
If you just want to do redirects, then .htaccess or a plugin is probably the way to go.
If you really want to use a rewrite rule for a redirect, then what you need to do is rewrite the URL to set a custom query var, then check that query var in the template_redirect
hook, and perform a redirect.
First up, we'll create a custom query var called wpse_283104_redirect. A query var is a query string that WordPress recognises. We need to tell WordPress to see
?wpse_283104_redirect=1in a rewrite so that we can check it later. This is done by filtering the
query_vars` array:
function wpse_283104_query_vars( $vars ) {
$vars[] = 'wpse_283104_redirect';
return $vars;
}
add_filter( 'query_vars', 'wpse_283104_query_vars' );
Next is the rewrite rule:
function wpse_283104_rewrite() {
add_rewrite_rule( '^testpage/?', 'index.php?wpse_283104_redirect=1', 'top' );
}
add_action( 'init', 'wpse_283104_rewrite' );
Now /testpage/
will load the homepage, but since we registered wpse_283104_redirect
we can see if we're on /testpage/
(or whatever the rewritten URL is) with [get_query_var()][2]
.
So inside the template_redirect
hook we'll perform this check and redirect:
function wpse_283104_redirect() {
if ( get_query_var( 'wpse_283104_redirect' ) == '1' ) {
wp_redirect( 'http://example' );
exit;
}
}
add_action( 'template_redirect', 'wpse_283104_redirect' );
index.php
. Have you checked the official codex page for this function? It's pretty straight forward. – Johansson Commented Oct 16, 2017 at 19:25/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. – Jacob Peattie Commented Oct 17, 2017 at 12:30