Apologies if this is answered in the documentation or elsewhere on the forum - I didn't see anything I understood, and didn't know what to search for.
Basically, I have categories with a name and an ID - I use the ID in the querystring to control page contents, but want to rewrite the URL to include the category name in place of the category ID. Thus, for example, assuming my database contains a category
of ID 3 and name Mens Clothing, the umodified URL would be shop.asp?cat=3, and I want to change it to read
Can I create a file listing the associations, and reference that somehow? How is this done?
P.S. Also please note I've yet to do any ISAPI rewriting, so if you can provide an explanation, please take this into account.
Jun 4, 2007 at 2:58 PM
Edited Jun 4, 2007 at 2:59 PM
You won't be able to do what you want since you need to pass the ID variable to your processing page.
You could, however, do this:
your rule would look like this:
RewriteRule ^/(\w+)/(\d+)/(\w+)/$ /$1.asp?id=2
RewriteRule ^/(\w+)/cat(\d+)/(\w+)/$ /$1.asp?cat=$2
So it reads: any url with /page/cat+id/title/ is processed by /page.asp?cat=id. The title is just for "looks".
(\w+) captures 1 or more word/alphanumeric characters
(\d+) captures 1 or more digit characters
To learn more about Regular expressions, I recommend Expresso, a freeware builder, tester, analyzer. It's great! Also check out ILoveJackDaniels.com for some great cheat sheets.
Basically URL rewriting captures what's being requested, checks if it matches any of the rules and if the rule matches what's being requested, it secretly processes the page according to the rule, leaving the requested URL alone.
With the rules, you're creating a Regular Expression to capture the portions of the requested URL as variables to be passed to the processing page; hence the $1, $2, $3. The manual spells this out.
Jun 4, 2007 at 5:24 PM
Edited Jun 4, 2007 at 5:26 PM
Hi! Thanks for helping me out!
So, if my desired url were http://www.domain.com/3/mens-clothing, I can use a rule to say that any URL like that is actually processed as if it where
http://www.domain.com/index.asp?cat=3 if I use:
RewriteRule ^/(\d+)/(\w+)$ /index.asp?cat=$1
In the above, $1 references
(\d+), as this is the first match element. $2 would reference
So I've seen various sites that manage to get around having to have the id value present - I'm sure they're not selecting content from the database by matching the title, so how do you think they're doing this?
Thanks again for your help,
P.S. (\w+) doesn't match the dash in mens-clothing - it has to be
(\w+-*) i think, making
RewriteRule ^/(\d+)/(\w+-*)+/$ /index.asp?cat=$1
Yep... that rule should get you want you want.
FYI...if you want, you could use an underscore instead of a dash as the
(\w+) rule will capture it.... but you are right with the rule to capture the dash.
If you're not capturing the title (not using the $2), you can do the following:
RewriteRule ^/(\d+)/(?:\w+-*)+/$ /index.asp?cat=$1
It just matches the expression but doesn't capture it...but that's up to you...
I'd also suggest adding the forward-slash at the end of the URL, but if you don't you can modify your rule as such to look if it's there or not:
RewriteRule ^/(\d+)/(?:\w+-*)+(?:/?)$ /index.asp?cat=$1
I'm not sure how those other sites do it. Maybe they're doing a look-up via title....Don't know.