2

I'm building a little database driven PHP CMS. I'm trying to figure the best strategy for this case scenario:

I have a URL like this:

http://www.my.com/news/cool-slug

Someone saves or share this URL (or it gets indexed by Google).

Now I realize that the slug is not quite right and change it to:

http://www.my.com/news/coolest-slug

Google and users who previously saved the URL will hit a 404 error.

Is this the best and common solution (showing the 404) or should I keep a table in my database with all the history of the generated URLs mapped to the ID of the page and redirect with a 301 header? Will this be an unnecessary load on my system (this table can get lots of records...)?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
fana
  • 374
  • 7
  • 17
  • Depends on the site really, traffic-wise and their user base. It could be more performant to just store it's previous slug next to the current one rather than a full history for everything. – MDEV Jan 10 '15 at 15:38
  • Possibly related: [How to make a redirect in PHP](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php/768472#768472) – mbschenkel Jan 10 '15 at 15:40
  • But should I worry and build a system to redirect old URLs to the new ones or should I just let them hit a 404? What do systems like Wordpress do? – fana Jan 10 '15 at 21:17

1 Answers1

0

One very common solution used by many sites (including StackOverflow as far as I can tell) is to include the ID in the URL. The slug is just here for SEO/beauty/whatever, but is not used to identify the page.

Example: http://stackoverflow.com/questions/27877901/strategy-for-permanent-links-not-wordpress

As long as you have the right ID, it doesn't matter what slug you use. The site will just detect that the slug is wrong, and generate a redirect to the right one. For example, the following URL is valid:

http://stackoverflow.com/questions/27877901/old-slug

If for some reason you do not want the ID in the URL, then either you forbid changes (many news site do that: you can notice that sometimes the slug and the title of a news article do not match), or your have to live with the occasional 404 when the slug changes. I've never seen any website having a system to keep the slug history, as it can be quite annoying (you won't be able to "reuse" a slug for example).

rlanvin
  • 6,057
  • 2
  • 18
  • 24
  • Wordpress keeps track of these slug changes for posts. It uses the `wp_postmeta` table and adds the `_wp_old_slug` key related to the post_id. I think that it searches this table if no other match is found before it shows a 404. – fana Mar 23 '15 at 22:05