February 22nd, 2012, 09:53 AM
Omitting IDs in mod_rewrite rule?
Hi guys, is it possible at all to hide the ids in my mod_rewrite rule?
So give the sample url:
Anyway we can make this:
I've read up and it 'seems' as if we need each ID visible in the URL to show the specific data assigned to these IDs, but doesn't that defeat the point of mod_rewrite?
February 22nd, 2012, 03:35 PM
The best way would be to make your game.php accept titles as well as/instead of IDs.
As it is now, no you can't. mod_rewrite only rewrites the URL. It can't add information that isn't there to begin with... unless you use RewriteMap, but that takes effort, and if you're putting in effort you might as well go for the simpler solution.
February 22nd, 2012, 03:57 PM
Yes, showing the IDs for rows in the database does partially defeat the purpose, but it still results in URLs that look better than those with query strings.
You don't have to include the ID, but it's more efficient to do so because you can directly extract it from the URL and use it in a well-optimized DB query. If you are just using the title, then a DB query will typically take longer, or for a static list of categories, you could keep a hash-table (e.g. an associative array) in your code for faster look-ups.
As with many other things in web development, you have to find the right balance for your site.