Undefined Variable
Undefined Variable

Reputation: 4267

Human friendly Unique URL

So we have this "bloggish" site for which we are building a better URL scheme. The basic idea is to convert the URLs to a human and SEO friendly structure. I have a function which generates the URL slug based on the title of the posting.

A record looks kind of like this in the database now:

    post_id         post_title              post_url_slug
--------------------------------------------------------------       
    1               Hello World             hello-world
    2               Another Post            another-post

So now we can change

http://<site>/post.cgi?action=read&id=1

to http://<site>/post/read/hello-world

In order to ensure that the url is unique, we are planning to append the post_id to it.

So now http://<site>/post/read/hello-world

becomes http://<site>/post/read/hello-world-1

All of this works very well. My main question is how should we handle the situation if the blog poster edits his title? We wouldn't want hello-world-1 to be showing a totally unrelated post.

The obvious solution is to recreate the slug when the title is edited. But how will we handle user bookmarks/google tracking/SEO crawling? We could do a 301 redirect, but keeping a whole history of URL slug changes and redirecting them like that can quickly become unmanageable.

Also I would really prefer keeping the routing/rewriting/redirections within the application and not a custom on the fly .htaccess or something. The reason being that if it is in the application and something goes wrong only some users will have issues, but we all know what happens when files like .htaccess catches fire!

Does anyone have any insight/experience managing such scenario? Any inputs are very welcome!

Upvotes: 0

Views: 333

Answers (2)

RichardB
RichardB

Reputation: 2767

I am assuming the pages are being created dynamically based on the URL, in particular the id. In which case, why not have the page id as its own segment, ie

http:///post/read/1/hello-world

You could then either allow http:///post/read/1/another-hello-world to show the same page, or make it so that your router checks that the id matches the title, and if it does not, it redirects to the correct URL.

If every wrong title redirects to the right URL then you would not have to keep a history of changes.

Unless it is unique, the title should not be being used to say what content to show, the unique id should.

You could also do the http:///post/read/hello-world-1 method by casting "hello-world-1" as an integer to retrieve the "1". That would mean you would have to filter numbers from the page title, however, and I personally think it looks a messier URL.

EDIT: I suspect this is what Stack Overflow are doing stackoverflow.com/questions/24637589/helloworld redirects to this page - they just need the "24637589" to know what page and url to show.

Upvotes: 1

oliakaoil
oliakaoil

Reputation: 1689

The short answer that you already know and won't like is that you have to keep track of slug changes and modify your app to respond accordingly if you want to keep your current URL scheme untouched. There's really no other way to keep "old" slugs active outside of keeping track of them in some fashion and adding them to your routing table. I would suggest keeping "old" slugs in a separate store which is designed specifically for this purpose so you can route them efficiently. You could then create somewhat efficient routing logic which says "Is this slug recognized? Yes => Route, No => Is this an old slug? Yes => Route, No => Slug not found.

Upvotes: 0

Related Questions