Forum Moderators: phranque
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! ;)
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
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.
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