2

My environment is Apache, DB is mySQL and language is PHP.

What I need is to create a middle layer which intercept the request and check the urls such as "http://www.something.com/apple/ipad-2-cases/" and then look up the db and where both apple and ipad-2-cases has a counter such as below:

Products Table

  1. p_id --> 1
  2. p_name --> "iPad"
  3. p_custom_url --> "ipad-2-cases"

Brands Table

  1. b_id --> 2
  2. b_name --> "Apple"
  3. b_custom_url --> "apple"

and convert the url into "http://www.something.com/products.php?b_id=2&p_id=1" from where I can grab b_id and p_id to bring out appropriate product info.

It basically quite looks like the way implemented in Joomla. You can define your own user-friendly url by typing an extra field otherwise it implements default setting anyway.

Since I am new to PHP, I don't quite know how to accomplish this. IN asp.net I've been using URL-Writer library which uses HttpHandlers and HttpModules to intercept the current request and make proper replacements.

Is there any library or something out there which can help me out on this one? Or do I need to use .htaccess (I know I need to turn on the mod_urlwrite on .htaccess)

Thanks a lot.

1

1 Answer 1

2

Instead of ids, use names.

Using mod_rewrite in .htaccess go from

http://www.something.com/apple/ipad-2-cases/ 

to

http://www.something.com/products.php?b=apple&p=ipad-2-cases

and find the relevant ids inside products.php

EDIT
In .htaccess create a rule like this:

RewriteRule ^[^/]+/([A-Za-z_-]+)/([A-Za-z_-]+)$ products.php?b=$1&p=$2 
Sign up to request clarification or add additional context in comments.

3 Comments

Names might not be unique and that cause a problem for me but seems a good idea, still it is not the answer tho ;)
And also I am talking thousand products here and writing all of these takes quite a time and parsing .htaccess for such a product mass could take time and cause slow response time.
No, just create a rule in .htaccess. Check my answer now. As for the uniqueness - you will have to ensure this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.