How To Change WordPress Permalink Structure with Permalink Redirect Plugin

It has to be one of the most frequently asked questions here. “I have changed my WordPress Permalink Structure, and now all my internal/external links are broken. My most popular posts have dropped off from the Google SERP completely, and I am loosing thousands of dollars per day! How can I fix them?” Well, maybe not the thousands of dollar part (I am trying to exaggerate as most people do on the Internet), but you get the issues associated with permalink changes. It seems that some people have pointed to my Permalink Redirect plugin as “the solution”, but it is actually not that straight forward. I shall try to look at the problem and the solution in this post.

People change their mind. It might be a mis-configuration in the beginning, or semantic change of the site structure, or just for the sake of trying out something new. The fact is, people do change their permalink structure in WordPress, which introduces many issues after the change. For example,

  • All internal links to older posts are broken. It might be fixable by re-editing these posts with internal links.
  • All external links to older posts are broken. It might not be as easy to change as you do not have control over the sites on the providing side of links.
  • Search engines will start to hate you because of all these broken links. Especially the hard earned ones from trusted sources.

So, how can we change the permalink structure and keep all your links intact?

Solution involves a bit of Apache mod_rewrite and the permalink redirect plugin. The principle is simple.

  1. Use mod_rewrite to convert the old URL into query string URL.
  2. Use Permalink Redirect plugin to redirect the 2nd request to the new permalink.

For example, we are changing from /%year%/%monthnum%/%postname%/ to /%category%/%postname%.html, and a visitor requesting /2007/02/this-is-a-test/ would get:

  1. Initial request: http://wpsite/2007/02/this-is-a-test/
  2. 1st redirect: http://wpsite/?name=this-is-a-test
  3. 2nd redirect: http://wpsite/foo-bar/this-is-a-test.html

Implementation Details

Some assumptions so I don’t need to explain basic concepts:

  • Setting up permalink structure in WordPress.
  • Editing .htaccess file inside your WP installation.
  • Understanding of mod_rewrite rules and regular expression.

First of all, you need to identify the structure tag that uniquely identify each individual post in your old permalink structure. It is either %post_id% or %postname%. Do note that from the database level, wp_posts.post_name does not have to be unique, although WP forces a suffix when post name is not unique (in app, not enforced by DB).

For example, /%year%/%monthnum%/%postname%/ would be the %postname%, and /archives/%post_id%.html would be the %post_id%. The first task would be writing a mod_rewrite rule to capture these structure tags and redirect to default query string URL.

Permalink Structure /%year%/%monthnum%/%postname%/ /archives/%post_id%.html
Redirect To /?name=%postname% /?p=%post_id%
Mod_Rewrite Rule RewriteRule /[0-9]{4}/[0-9]{2}/([^/]+)/? /?name=$1 [R,L] RewriteRule /archives/([0-9]+)\.html /?p=$1 [R,L]

You might wish to test whether your Mod_Rewrite rule is working by going to the old URL. It should then redirect you to the new query-string-based URL. It should also be possible to achieve the same with other web servers like Lighty or Nginx. YMMV.

The next step is easy — just install the Permalink Redirect plugin and activate it. What it does is, whenever a page/post is requested with a different permalink than the one defined in the permalink structure option, it will redirect the request to the canonical permalink. So when the post is loaded with query-string-based URL, it will then redirect the visitor to the new URL based on the new permalink structure.

Conclusion

Sorry I have to apologise for this rough draft on solving the permalink issue. It might make sense to some but not to others as I have left out details such as mod_rewrite syntax, regular expression syntax, etc. The functionality could have been implemented in a plugin and WP has already provided most of the functionalities (in wp-include/rewrite.php), however I do not see burdening WP for things Apache mod_rewrite could do very quickly a good solution.

Again, your mileage may vary. It’s also a good idea to come up with a suitable permalink structure right from the beginning to avoid all this mess :)

Category: General | Mon, 19 February 2007 11:08 am
Tags:

Links to This Article

  1. Thu, 5 April 2007 7:38 am
    SDAC Inc: SDAC Recommends: WordPress Plugin: Permalink Redirect Plugin
  2. Wed, 5 September 2007 6:11 am
    Get multiple DMOZ links and use Wordpress to do it | Needless Productions
  3. Thu, 9 October 2008 11:26 am
    关于Wordpress重定向及相关插件 | Riku

Comments

1.
Avatar for Mega Millions
Posted by Mega Millions on Mon, 23 July 2007 10:08 pm

Hey,

Try the permalinks migration plugin. You don’t need to use mod rewrite to get that working.

Here’s my experience with changing wordpress permalinks


2.
Avatar for Enrique Bergemann
Posted by Enrique Bergemann on Sat, 8 September 2007 5:00 pm

Dear Scott,

just two little words for a great (permalink-)solution: Thank You!

Enrique


3.
Avatar for Ryan
Posted by Ryan on Sat, 1 December 2007 8:27 am

“… Try the permalinks migration plugin. You don’t need to use mod rewrite to get that working.”

Ironically, the link you provided gives a 404!


4.
Avatar for Malaiac
Posted by Malaiac on Tue, 5 February 2008 6:35 am

Just FYI, the actuel wp_redirect of WP 2.3.2 is bugged
Function is
function wp_redirect($location) {…}
I had to manually add a 301 header to get things right
file is /wp-includes/pluggable-functions.php, around line 248

your plugin is not involved, nice work btw ;)


5.
Avatar for Dunn
Posted by Dunn on Wed, 26 March 2008 6:39 pm

I use your plugin to redirect from my old permalink /%year%/%monthnum%/%postname%.html to the new one without .html /%year%/%monthnum%/%postname% but it’s not working. Any suggestion?


6.
Avatar for patoune
Posted by patoune on Sat, 5 April 2008 10:15 am

I have problem to redirect from my old permalink with .htm

Only old permalink with .htm does not work.
Any idea?


7.
Avatar for John-Henrique
Posted by John-Henrique on Thu, 24 April 2008 7:56 am

I have a problem with permalink migration on wordpress

I always used permalink struture /%postname%/ but I have to use /%category%/%postname%
Its works correctly, but, users from Google Search seem page not found (error 404) How to avoid it?

Falopa!


8.
Avatar for rohit
Posted by rohit on Wed, 11 June 2008 1:54 am

I guess to make permalink structure change work, you need to have mod_rewrite php function enabled on your server. My website (rohitblog.com) is hosted on Windows/ PHP/ Wordpress- where mod_rewrite is not enabled. Any solution? thanks in advance.


9.
Avatar for Richard
Posted by Richard on Fri, 13 June 2008 1:13 pm

Scott, I really like the article and the clear explanation of the situations that arise. I am currently changing my permalink structure (i was using a windows host that required an “index.php” but now I am not) and find this info very helpful.

I am going to try to do this tonight…hope it works :)

Thanks for the article,
Richard


Avatar for Abhinav Sood
Posted by Abhinav Sood on Thu, 14 August 2008 7:32 am

I am on a Windows IIS based server but I somehow managed to set up Pretty Permalinks over the default ugly permalinks that I have been using for quite some time now!

I am using your plugin to 301 redirect my old links to new links, but I am facing a weird problem.

My homepage keeps on refreshing in a loop, automatically?

Can you please help me with it, as its increasing my bill for the excessive bandwidth consumption!

If you can reply by email, that would be great…

Regards,
Abhinav Sood.


Avatar for graphic art
Posted by graphic art on Wed, 3 September 2008 5:17 pm

I tried using it but I got a 404 error/page not found.

I used the /%postname%/ sa the new permalink from the default permalink in wordpress. Im hosting my wordpress with godaddy and I dont have access to my .htaccess


Add a comment

Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.