301 redirect through PHP in Wordpress

by pro101
3 replies
Hi all:

I've recently discovered that using /%postname%/ only for permalinks on posts in Wordpress is not scalable. My site has currently a small number of posts (25) and about 35 pages. Likely I will eventually have 100s of posts, so before it's too late it seems I need to change Permalinks.

Initially tried to look into htaccess rewrite, but Apache experts said that htaccess can't know postid, so the 301 redirect needs to be done through PHP script. I'm looking into PHP files in wordpress and wondering which file (and line of code) I need to change to accomplish this.

Anyone knows what I need to change in wordpress to redirect from:
from domain.com/postname
to domain.com/postid/postname

It would help a lot. Thanks in advance.
#301 #php #redirect #wordpress
  • Profile picture of the author Bruce Hearder
    Seeing your list of posts is so small (~25), why don't you just manually hard code the redirects into your .htaccess file right now, and then be done with it?

    All you will need is a line for each redirect at the top of your .htaccess file that is something like :

    redirect 301 /some-old-post-title/ http://www.yourdomain.com/123/some-old-post-title/

    That should do it..

    Take care

    bruce
    {{ DiscussionBoard.errors[497138].message }}
  • Profile picture of the author MemberWing
    Here it goes:
    WP Admin panel->Settings->Permalinks -> "Custom Structure" = /%post_id%/%postname%/

    Although for SEO friendliness I'd suggest using this:
    /%category%/%postname%/

    More info here:
    Using Permalinks WordPress Codex

    Regards,

    Gleb Esman
    {{ DiscussionBoard.errors[498982].message }}
  • Profile picture of the author justinkaiser
    I used to use the category/postname as mentioned above. I'd like to return to that configuration but have about 200 posts. Is there an easy way/plugin to do a 301 redirect?
    {{ DiscussionBoard.errors[502453].message }}

Trending Topics