Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite with forward slash /

Problem with mod rewrite

         

lotosfish

10:28 pm on Oct 13, 2005 (gmt 0)



My site has automotive recall data and I have a mod rewrite that rewrites the urls like /MAKE/YEAR/MODEL/ID to manufacture=%1&year=%2&model=%3&recordid=%4. It works except when the model contains a /. The PHP encodes it, and the url would look something like this:

http://www.example.com/recalls/CHEVROLET/2000/C%2FK+PICKUPS

This is the code I have:


RewriteEngine on
RewriteRule ^([A-Za-z0-9\+-]+)$ /recalls/?manufacture=$1
RewriteRule ^([A-Za-z0-9\+-]+)/([A-Za-z0-9\+-]+)$ /recalls/?manufacture=$1&year=$2
RewriteRule ^([A-Za-z0-9\+-]+)/([A-Za-z0-9\+-]+)/([^/.]+)$ /recalls/?manufacture=$1&year=$2&model=$3
RewriteRule ^([A-Za-z0-9\+-]+)/([A-Za-z0-9\+-]+)/([^/.]+)/([A-Za-z0-9\+¦\.-]+)$ /recalls/?manufacture=$1&year=$2&model=$3&recordid=$4

Can anybody give me a hint?
Thanks!

[edited by: jdMorgan at 11:20 pm (utc) on Oct. 13, 2005]
[edit reason] example.com [/edit]

jdMorgan

11:20 pm on Oct 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lotosfish,

Welcome to WebmasterWorld!

You'll need to reverse the order of your rules so that the most-specific rule is first, and then remove the "/" from the negative-match patterns used to extract the model (first two rules below):


RewriteEngine on
RewriteRule ^([a-z0-9\+-]+)/([a-z0-9\+-]+)/([^.]+)/([a-z0-9\+¦\.-]+)$ /recalls/?manufacture=$1&year=$2&model=$3&recordid=$4 [NC,L]
RewriteRule ^([a-z0-9\+-]+)/([a-z0-9\+-]+)/([^.]+)$ /recalls/?manufacture=$1&year=$2&model=$3 [NC,L]
RewriteRule ^([a-z0-9\+-]+)/([a-z0-9\+-]+)$ /recalls/?manufacture=$1&year=$2 [NC,L]
RewriteRule ^([a-z0-9\+-]+)$ /recalls/?manufacture=$1 [NC,L]

This will work as long as you don't have any URLs that contain *more* parameters than can be handled by the first rule.

Note also that I added the [NC] flag to each rule, making the pattern-match a case-insensitive compare. This eliminates the need to have both "A-Z" and "a-z" in your patterns, and speeds up processing. Also, I added the [L] flag to each rule, so that if the rule matches and is invoked, mod_rewrite rule processing will stop and the content-handler will be invoked sooner.

If possible, you should modify your script so that "/" characters in model names are escaped before being published as links. This will eliminate the need to allow slashes in the parameter patterns above.

Jim