URL Rewriting without ID in URL

Posted on 2009-02-14
Last Modified: 2013-12-12
I have seen websites seeming to use mod_rewrite to rewrite their URL's and they look like this:

The only way I know to do URL rewriting produces this:

How is the first result produced? Do they have a script that writes directly to the .htaccess file?
Question by:EMB01
    LVL 14

    Accepted Solution

    Chances are, they're just using a unique search key as the field to search on.

    For example, you can create a "slug" field in your products table and set it to UNIQUE.

    In your example, the product slug could be "Some-Product". As long as no other product has the same name, you will always be able to uniquely search for that product. Text searches won't be as fast as ID searches, but you'll still be able to select products by that field if you want a URL without an ID.
    LVL 34

    Assisted Solution

    by:Beverley Portlock
    Also they may be using URL rewriting via Apache to convert the URL into something like

    at which point looking up the database becomes a trivial issue. The mod_rewrite rules for Apache for here and the above could be achieved with something like

    RewriteEngine on
    RewriteRule ^/([a-zA-Z]+)/[-a-zA-Z0-9]+)\.html$ /index.php?slug=$2&cat=$1 [L]
    LVL 14

    Author Comment

    So, from the example, slug=$2 doesn't have to be numeric but it can be alpha?
    LVL 34

    Expert Comment

    by:Beverley Portlock
    It could be anything. By using mod_rewrite's regex capability you can pick up whatever you desire. For instance, to limit "slug" to just numbers I would change it from

    RewriteEngine on
    RewriteRule ^/([a-zA-Z]+)/([-a-zA-Z0-9]+)\.html$ /index.php?slug=$2&cat=$1 [L]


    RewriteEngine on
    RewriteRule ^/([a-zA-Z]+)/([0-9]+)\.html$ /index.php?slug=$2&cat=$1 [L]

    I've also just noticed a missing bracket in the original post which I have corrected. I have not tested these actual expressions posted here so if you use them you may have to tune them a bit. I was only attempting to illustrate the point but they should not be correct or very nearly so.
    LVL 14

    Author Closing Comment

    Thanks for the slug concept and the examples. I really need to learn regex!

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now