URL Rewriting without ID in URL

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?
LVL 14
Who is Participating?
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.
Beverley PortlockCommented:
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  http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html 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]
EMB01Author Commented:
So, from the example, slug=$2 doesn't have to be numeric but it can be alpha?
Beverley PortlockCommented:
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.
EMB01Author Commented:
Thanks for the slug concept and the examples. I really need to learn regex!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.