I've had this doubt for some time and now that I'm developing a website from scratch I'd like to learn how to make my web catalog URLs friendly and descriptive. something like this.
Instead of www.mysite.com/product.php?id=8799
Something like www.mysite.com/super-videocard-with-super-features
Or even www.mysite.com/products.php?ref=videocard-with-super-features
As long as they're friendly and descriptive I don't mind a .php?
thing.
How do they do it in e-commerce CMS to show URLs like these?
Here is what I've been thinking:
1) Storing the URL in MYSQL in the products table, a column called URL that stores a value like "super-videocard-with-super-features" (may be formating the title of the product and storing it), then, when fetching the products, making the links look like <a href="videocard-with-super-features">Click here to see product</a>
2) Using an HTACCESS file and everytime a product is inserted in the database PHP takes the HTACCESS file, and appends a rewrite rule... but this seems to be hard to do, and what happens when you have to update a product title? how to find that line in the HTACCESS file without having to write a super complex algorithm?
Any ideas of how to achieve this like they do it in a serious e-commerce application?
What you are looking for is something called a 'Front Controller' - a script which handles all requests and routes appropriately.
You don't want to mess with rewrites other than sending all requests to your controller (or one of a set - for example a product controller or a category controller). The controller itself can handle the logic of what the request url means by accessing this through $_SERVER['REQUEST_URI'] superglobal and either hardcoding routes (request URL to actual executing code) or fetching configs from DB or whatever.
Have a look at this tutorial: http://www.sitepoint.com/front-controller-pattern-1/
You can use a .htaccess file, but it's nowhere near as complicated as you think :)
Hiding the .php extension on files:
RewriteEngine On
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
Then construct your links like <a href='product'>
instead of <a href='product.php'>
.
And hiding GET parameters:
RewriteEngine On
# turn on the mod_rewrite engine
RewriteRule ^product/([0-9]+)$ product?id=$1 [L,QSA]
# Make /product/123 an alias for /product?id=123
RewriteRule ^product/([A-Za-z0-9_\-]+)$ product?ref=$1 [L,QSA]
# Make /product/productName an alias for /product?ref=productName
Again, you then need to modify your existing links that are set up like <a href='/product.php?ref=videocard-with-super-features'>
and change them to <a href='/product/videocard-with-super-features'>
.
Make sure the rewrite to remove the .php extension is first in the file, if you're using both.
1 would probably be easier to do.. and it only takes some effort by checking your database what the URL id is pointing to..
lets say i send the url to a stored procedure. thus
exec [someProcedure] 'videocard-with-super-features'
the stored procedure will then get the tagged ID of the string 'videocard-with-super-features' and get the correct data tagged along with it