views:

25

answers:

1

I have googled a lot, read a lot but still cannot figure out the basic concept behind pretty url and mod_rewrite.

I am currently making pretty url the php way. Like this

a) creating a field in a table with the name same as the text (or the title which i want in the url) separated with (-).

b) making hyperlink with the same field value like (a href="my-page-name")

c) and when passed through querystring checking the value from the database and if it matches, get the id for that row and do other stuffs...

So far it is working.. but I am confused.

IS THIS THE RIGHT WAY ? OR it can be controlled through .htaccess, since I am doing the same thing in all the pages which needs pretty URL.

Someone guide me please..

Thanks for reading ;)

A: 

The implementation is up to you, .htaccess would contain rules that would match a part of the requested URL and pas it to your script, and then it's up to you to do what you want, usually when using url rewriting the id is retained, but they add the "clean" version of the title to it, like:

the .htaccess will match both id (1) and clean title (my-first-post), and pass them to your page, which in turn should grab the parameters get the page with that id and display it. This way page uses takes advantage of pretty URLs without losing the quick id lookup.


Notes:

  • When using the mentioned example, you are advised to check whether the given clean-title is actually the same you have (you may cache that in a field in the posts table), if it was different, you should redirect the user to the page with the real title. Example: http://example.com/blog/1-wrong-title would redirect to http://example.com/blog/1-my-first-post.
  • It's just a suggested implementation, if you want you can ignore the id in the url, and depend on the cached clean-title table field to lookup the article, you should of course consider uniquely-indexing that field for fast lookups, an example of that approach is Wikipedia's (Mediawiki).
aularon