#1
  1. *Programmer In Training*
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    SE London
    Posts
    371
    Rep Power
    12

    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:
    Code:
    game.php?game_id=1&category=1
    Anyway we can make this:
    Code:
    /GAMETITLE/CATEGORYRTITLE/
    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?

    Regards,
    Joe.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,745
    Rep Power
    9397
    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.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,122
    Rep Power
    4258
    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.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo