Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

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:
http://www.somewebsite.com/products/Some-Product.html

The only way I know to do URL rewriting produces this:
http://www.somewebsite.com/products/Some-Product-P1.html

How is the first result produced? Do they have a script that writes directly to the .htaccess file?
0
EMB01
Asked:
EMB01
  • 2
  • 2
2 Solutions
 
crackyCommented:
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.
0
 
Beverley PortlockCommented:
Also they may be using URL rewriting via Apache to convert the URL into something like

http://myDomain.com/index.php?slug=Some-product&cat=products

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]
0
 
EMB01Author Commented:
So, from the example, slug=$2 doesn't have to be numeric but it can be alpha?
0
 
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]

to

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.
0
 
EMB01Author Commented:
Thanks for the slug concept and the examples. I really need to learn regex!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now