[wp-hackers] Rewrite rule for pages from old site

Otto otto at ottodestruct.com
Fri Jun 22 13:23:22 UTC 2012


On Fri, Jun 22, 2012 at 8:03 AM, Brenda Egeland
<brenda at redletterdesign.net> wrote:
> Sermon is a custom post type. I've tried add this to my functions.php for a custom rewrite rule:
>
>  add_rewrite_rule(
>    '^/sermons/sermon([0-9]{4})([0-9]{2})([0-9]{2})\.php?',
>    '$matches[1]/$matches[2]/$matches[3]?post_type=sermon',
>    'top'
>  );
>
> But I keep getting a sorry...not found. I've tried resaving the permalinks in the Settings panel, and still no luck.

That isn't how rewrite rules work.

You're trying to rewrite the old .php based URL into the new "pretty"
url, but what a rewrite rule in WP does is to convert a "pretty" URL
into a "default" URL. Default is that thing with ?p=123&year=2012 and
so forth.

In other words, you can do what you want, but the second part of the
rewrite rule needs to be "non-pretty".


 add_rewrite_rule(
    '^/sermons/sermon([0-9]{4})([0-9]{2})([0-9]{2})\.php?',
    'index.php?post_type=sermon&year=$matches[1]&month=$matches[2]&day=$matches[3]',
    'top'
  );

Or something like that. Didn't test it specifically, might be wrong.

But that won't redirect the old URL to the new one, that will just
make the old URL work and show the proper content.


If you're wanting to actually REDIRECT from the old format to the new
one, then you're doing it wrong. add_rewrite_rule can't do that
directly. (Well, it *can*, it's just a bit pointless to do it that
way). If you want to redirect from the old to the new, then putting a
RewriteRule directly into the .htaccess (as Mika suggested previously)
is the simpler and easier way.

-Otto


More information about the wp-hackers mailing list