0

I currently have my wordpress blog (apache2, ubuntu linux) hosted at e.g. mysite.com using ID-based linking, e.g. http://mysite.com/?p=547. I have two goals:

  1. move my blog to mysite.com/blog
  2. change the link structure to /{post-title} instead of /?p={post-id}

Moving to the new structure should be simple enough, as is changing the setting in wordpress to use the new permalink structure.

What I would also like to do is not break the random(1..100) sites that currently link to me. I would like to use an HTTP 301 permanent redirect, but I'm not sure how to do that by changing apache's settings.

The first issue will be to handle the redirection to /blog/ for all requests.

The second issue will be to handle the new link structure. I figure I can query the mysql database easily enough and generate something like ?p={post-id} > {post-title}, but I'm not sure what the syntax should look like and where I need to put it.

Any help would be much appreciated.

Ben McCormack
  • 705
  • 4
  • 9
  • 16

1 Answers1

3

When you change the URL scheme from p=123 to post-title, wordpress will automatically 301 visitors from the old ID URL's to your correct url - so that's sorted out.

Next, since your stuff is reached via ?p=[0-100] then you could quite easily take care of this via PHP. There are 2 solutions as I see it - one is a bit more elgant than the other.

  1. Write some code that looks into the Wordpress database according to the pageid in GET, and 301 the user accordingly.

  2. Do a 1 to 1 301 redirect, something like

<?php
if(isset($_GET['p']) && is_numeric($_GET['p'])) {
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.mydomain.tld/blog/?p=".$_GET['p']);
}
?>

The above code is untested, but should illustrate the idea. The above code will in many cases cause 2 301's - but as far as search engines, it should only send them through the 301 twice and make them update their URL's.

Bookmarks - you can't do much about those.

Frands Hansen
  • 4,657
  • 1
  • 17
  • 29
  • I tried changing my URL permalink structure and got 404s back. Perhaps Wordpress doesn't cover this? – Ben McCormack Aug 19 '12 at 23:57
  • 2
    @BenMcCormack You have to add [the WordPress-provided code](http://codex.wordpress.org/Using_Permalinks#Creating_and_editing_.28.htaccess.29) to your `.htaccess` file. – Michael Hampton Aug 20 '12 at 00:21
  • Yeah, what Michael says. You need to add the code to make Apache pass the requests to Wordpress, so Wordpress can handle them. – Frands Hansen Aug 20 '12 at 10:31
  • I found I also had to turn on rewrite using `a2enmod rewrite`. [This answer on StackOverflow](http://stackoverflow.com/a/5758551/166258) was quite helpful. – Ben McCormack Aug 20 '12 at 23:43