Using database to update rewrite rules

Hi,

let’s say I want to create nice URLs for every manufacturer on my site,
so I have
www.mysite.com/samsung
www.mysite.com/sony
www.mysite.com/acme

However, if I have hundreds of manufacturers and if they’re changing constantly, what then? There are some vague references for something called rewrite map somewhere but nothing that explains it and no tutorials. Can anyone help?

Also, why is this problem not the main topic covered in tutorials for mod_rewrite? How is mod_rewrite possibly useful when you have to maintain it manually (assuming you have new content on your site once in a while)?

OK,

Good questions! A RewriteMap is a “procedure” call which can define something as simple as the internal functions (tolower, toupper, escape and unescape) to providing access to flat file (mapping from one URI to another) to a database access to fetch the response (URI or otherwise) from the db based on the function call. The only problem with this is that an error in a RewriteMap can stop the server - at least prevent it from responding to other requests. As a consequence, it’s NOT something to be put in the hands of the unwary - ESPECIALLY on a shared server!

Was there a reason not to show your mod_rewrite for your manufacturers? After all, if they’re just sent to a handler to check for a manufacturer in a db, what’s the problem?

Regards,

DK

Thank you for your reply dklynn

I don’t understand this question:

What is ‘showing my mod_rewrite for my manufacturers’?

Here’s my problem. A typical URL on this site would be like:
www.mysite.com/productlisting.php?vendor_id=15&p_cat=25
… where vendor_id is the id od the manufacturer and p_cat is the category of the product. I’d like to convert the previous URL to something nice and readable like:
www.mysite.com/speakers/jbl
And that’s not a problem if I update the rewrite rules manually. However, if I have hundreds of manufacturers and dozens of categories and both are added and removed daily then it’s a problem.

On StackOverflow someone helpfully suggested I route all requests to a single PHP script that can figure out what the request was and include all the proper files, like:

RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]

and I’ve looked around for hours and this seems to be not just the best but the only way of doing this apart from using RewriteMap (which requires access to httpd.conf). How do I access httpd.conf? How do other sites do it?

My main question though is: Why is this problem not the main topic of every single tutorial about mod_rewrite? How is mod_rewrite useful without this? Plenty of sites, even crappy ones have nice URLs for dynamic content, it seems to be routine and I feel as if I’m missing something really basic here. No tutorials, no explanations anywhere (the one on StackOverflow was the only one I found and I only got one reply!) and everybody’s using mod_rewrite for dynamic content like there’s some obvious way of doing it that just doesn’t need an explanation.

Puzzled…

puzzled,

I’m puzzled as to why you haven’t read the mod_rewrite tutorial linked in my signature as it answers ALL your questions (yes, I’ve heard them all before and have included answers in the tutorial).

That means that you’ve not shown your code and the content and order is important. PLEASE show what you have in your .htaccess file so we’re better able to help you.

If you’ve read my signature’s tutorial, I discuss your problem(s).

  1. YOU create the new format of your URI

  2. YOU create the mod_rewrite code to convert your new format to something Apache can serve.

  3. My tutorial leads the reader through developing GOOD mod_rewrite code for that but notes that the IDs are a horrible thing to use in the links. THEREFORE, if you can be assured that the manufacturer name and product name are UNIQUE (which can be enforced in a MySQL database), then USE THOSE FIELDS in your new format AND in your redirection so your display script can query the database on those fields, not on the ID fields.

For example, I have a client which cannot guarantee unique titles (articles are contributed by outside sources) and one who manages his own articles. The later’s titles are used for links (http://wilderness-wally.com/) while the former is stuck with numeric links.

RewriteMap? Why? You just don’t need it if you use unique fields and query on those rather than IDs. As for a redirect.php, that’s what a RewriteMap would be using albeit without having to use header() calls. It’s not pretty but it is available if needed - but it’s not!

Regards,

DK

Well that’s a problem because I use id (int) to identify every item in the database. I use it because it’s faster to search for int than a string, because strings can and do change and ids don’t, because there may be items with same names, etc. I’m not sure the necessary changes and limitations are worth it just to have pretty URLs, but I’ll consider it, thanks. :slight_smile:

As for your tutorial, only now I managed to find the explanation about IDs and why not to use them. It’s somewhere in the middle, hidden under a section vaguely titled ‘mod_rewrite Links’ so it’s a bit hard to find. Why didn’t I read the entire tutorial? The same reason I don’t read any tutorial/manual in its entirety but only the parts I think I need. It’s a bad habit that I’m working on. :slight_smile:

OK,

As long as the MySQL field is unique, it’s searched as quickly as the numeric ID. However, if you cannot guarantee that the field is unique, (a) you’re not specifying it as unique in the db structure and (b) you can’t use that field to select a unique record. If you checked the Wilderness-Wally link, you’ll see that it’s well worth the minor effort.

Too true! The country/state/city example was to develop good regex (and NOT use (.*) ) so I must admit that the alternative to using a unique field rather than ID (another unique field) was well hidden. I’ll have to think about elevating that to a section heading to make it easier to discover with a quick look (the ONE PAGE is far too long - that’s why I encapsulate each topic in accordion sections).

Regards,

DK