Forum Moderators: phranque
I have successfully changed my dynamic links from a proprietery program to give me static urls. However, what I would really like is to have meaningful names instead of numbers.
Can one take a number and run it through something like a case statement i.e. if a category "9" appears then this will be transformed into the wording "mycategory"?
Then is it also possible to take the title of a page and display this on the url line i.e. the page is still a number but would like it to be known by it's worded title?
Is it possible then to do this with a script if not with .htaccess?
Thank you so much in advance for any replies.
trout
Welcome to WebmasterWorld!
I have successfully changed my dynamic links from a proprietery program to give me static urls. However, what I would really like is to have meaningful names instead of numbers.Can one take a number and run it through something like a case statement i.e. if a category "9" appears then this will be transformed into the wording "mycategory"?
This is possible to do to a limited extent using mod_rewrite in a per-directory (.htaccess) context, and to a somewhat greater extent using rewrite_map in mod_rewrite in a server configuration (httpd.conf) context. But either way, the solution becomes unworkable if more than a few categories are involved. For ten or twenty categories it would be OK; For more than 50, it would become cumbersome and difficult to maintain. With many categories, it would also begin to affect your server performance.
The basic problem is that the category numbers and category names must have a defined relationship - an association. This relationship must be know to the script that produces "friendly" URLs from the numeric category/product links for display on your pages, and it must also be known to mod_rewrite, which translates requests for named pages back to script calls for numbered categories and products.
So, the association must be defined and maintained in two places: the page-generation script and the URL-rewriting code. This places some practical limits on how far you can go with this method.
Then is it also possible to take the title of a page and display this on the url line i.e. the page is still a number but would like it to be known by it's worded title?
You could change your script to retrieve the title of the page being linked-to, and insert that into the URL after escaping special characters, but this would be a difficult methodology to maintain. If the page title were edited, the link would break because the mod_rewrite code would not know which numeric category the "new" title-URL is associated with. You would have to make changes in both places.
A better approach, if you have control over your database, is to create a "friendly-URL" data entry for each product and category, and use that to create the links on your pages. Then, change the script so that it can "select" products and cats using the friendly URL directly. This eliminates the need for any URL translation (mod_rewrite) and centralizes all product/cat link creation and maintenance where it belongs - in the database.
Jim
I wonder if I can query you further?
Do I understand from your post that it can be done purely by .htaccess and to forget about the script for now? (or at least until I have a greater understanding)
I have a dozen categories i.e. 1-12 and they are currently all known as "browse/1" instead of browse.php?category=1
How would I change browse/1 to aName, browse/2 to bName, etc, etc. or more simply browse.php?category=1 to aName etc.
I have really only begun to experiment with .htaccess and so am fairly unsure about things :( Are you aware of a tutorial specifically for where I would like to go?
Thanks.
trout.
No, you must change your script to "publish" friendly URLs on your HTML pages, so that users and robots see them. Then you must use mod_rewrite to recognize those friendly URLs when they are requested from your server, and translate them to the "unfriendly" URLs needed to call your script to generate the next HTML page. Both the script and the mod_rewrite code must be aware of the "associations" between the friendly and unfriendly URL values.
There are several basic resources cited in our forum charter [webmasterworld.com]. The ones concerning mod_rewrite and regular-expressions are pertinent to this problem.
Jim