Created
October 11, 2011 19:15
-
-
Save billerickson/1279091 to your computer and use it in GitHub Desktop.
How to handle redirects in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Otto: | |
Step 1: Just try it as is and see what happens. WordPress's canonical | |
redirection is pretty darned clever in some ways, and it often can | |
guess the right thing to serve out of the box. | |
Step 2: If the built in canonical redirection isn't able to detect and | |
redirect accordingly, then you can add your own code to discover and | |
handle the redirections. The way you do this is with the | |
redirect_canonical filter. | |
add_filter('redirect_canonical','my_redirects',10,2); | |
function my_redirects($redirect_url, $requested_url) { | |
// do stuff | |
return $redirect_url; | |
} | |
The function gets the $requested_url, which is what the person asked | |
for, and it returns the $redirect_url, which is what WordPress thinks | |
it should serve. | |
If the $redirect_url is false, then WP will not redirect and will go | |
to 404 instead. But if the $redirect_url is a normal URL string, then | |
it will issue a 301 redirect to that string. So if you change that | |
$redirect_url before returning it, you can make it redirect elsewhere. | |
So if you can write some code to do the mapping from the old URL to | |
the new ones, you can make a plugin that has that mapping and then | |
hook it in using this filter. Voila, instant 301 handling. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment