[wp-hackers] Content Migration from MT to WP - Redirects

Jonas Nordström jonas.nordstrom at gmail.com
Wed Oct 12 11:15:26 UTC 2011


On Tue, Oct 11, 2011 at 20:17, Otto <otto at ottodestruct.com> wrote:
> On Tue, Oct 11, 2011 at 12:55 PM, Brian Fegter <brian at fegter.com> wrote:
>> I'm working on a very large site that is built on Movable Type. We've
>> created MT templates that output proper WXR and the import is working great
>> in WP. What is the best practice for setting 301 redirects from old > new
>> permalink structure? The domain is not changing so we can't use MT templates
>> to do the redirects. Just wanted to get some collective wisdom before I
>> tackle this.
>
> 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.
>
> -Otto
>
If you're using nginx (many large web sites do) then there's a really
fast way to accomplish the redirect in the web server. Use map (
http://wiki.nginx.org/HttpMapModule ).

Example:

map $uri $new {
	/ab/product-32/		/product/billy/;
	/ab/product-33/		/product/ivar/;
	/ab/product-34/		/product/nicklas/;
	/ab/product-35/		/product/sommar/;
}
server {
    listen 1.2.3.4:80;
    server_name hostname.com;
	root        /var/www/wp;
	location /ab/ {
		rewrite ^ $new permanent;
	}
}

-- 
Jonas Nordström


More information about the wp-hackers mailing list