Forum Moderators: phranque

Message Too Old, No Replies

Troubleshooting mod_rewrite rule

rewrite rule not working as expected

         

doublespeak

11:07 am on Jun 5, 2005 (gmt 0)

10+ Year Member



Much as I hate to post a mod_rewrite question here, I'm stating to tear my hair out as to why this rule doesn't work.

One of my rules just won't do what I want or I expect it to do and I can't figure out why.

I want /search/keyword.htm to rewrite to /cgi-bin/search.cgi?q=keyword

I have the following rule:


RewriteRule ^search/([a-z]¦[A-Z].*).htm$ /cgi-bin/search.cgi?q=$1 [L]

This doesn't seem to do anything :(

If remove the htm extension and change it to


RewriteRule ^search/([a-z]¦[A-Z].*) /cgi-bin/search.cgi?q=$1 [L]

This gets rewritten to /cgi-bin/search.cgi?q= but only the first letter of the search term is carried over.

I guess I must be missing something obvious, but if someone could point it out to me it would definitely help my sanity! ;)

jd01

11:23 am on Jun 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi doublespeak,

RewriteRule ^search/([a-z]¦[A-Z].*) /cgi-bin/search.cgi?q=$1 [L]

Will match 1 lowercase letter from a to z or 1 CAPITAL A to Z then .* 0 to n characters that are not a line break.

This should work:
RewriteRule ^search/([a-z]*¦[A-Z]*)\.htm$ /cgi-bin/search.cgi?q=$1 [L]

This is more efficient:
RewriteRule ^search/([a-z]+)\.htm$ /cgi-bin/search.cgi?q=$1 [NC,L]

NC = no case.
+ = 1 or more... only slightly more effiecient, if you need to match 0 characters, use the *

If you know you will still match the pattern, and no wrong patterns - EG there are no numbers, - or / that should cause the rule to fail, this is much more efficient:
RewriteRule ^search/([^.]+)\.htm$ /cgi-bin/search.cgi?q=$1 [L]

([^.]+) = Anything that is not a literal .(dot)

\. Matches a literal .(dot) instead of 'any character except the end of a line'.

Hope this helps.

Justin

doublespeak

6:48 pm on Jun 6, 2005 (gmt 0)

10+ Year Member



Forgot to say - many thanks Justin :)

Seems I have quite a bit of work to get my mod_rewrite up to speed :)

One thing I'm slightly concerned about with my rewrite rules is 404s - essentially visitors can request URLs that don't and will never exist and get a 200 response. I guess I will need to work something out to handle those situations.

jdMorgan

3:12 am on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See RewriteCond, the variable %{REQUEST_FILENAME} and RewriteCond's -f and -d flags. Used carefully and very selectively, these tests for file-exists and directory-exists can be helpful in cases like yours.

However, you must make all efforts to avoid executing these tests unless all other tests match first. The goal is to avoid testing -f or -d unless all other qualifications such as URL, filetype, subdirectory, etc. are all matched. The reason for avoiding unnecessary use of -d and -f is that they cause accesses to the server filesystem and are therefore very slow and use a lot of server resources. Many sites run horribly slowly simply because of a popular example out on the web in which these tests are run without any pre-qualification, resulting in a disk search for every HTTP request sent to the site!

Jim