Forum Moderators: phranque

Message Too Old, No Replies

rewrite problem - required search and replace functionality

search and replace mod rewrite

         

adams station

9:40 am on Aug 18, 2008 (gmt 0)

10+ Year Member



This is the rewrite i am trying to perform:

results-jabra-a125-battery.htm

To

search/?searchterm=jabra+a125+battery

where 'jabra-a125-battery' is the search 'phrase'.

The phrase could be of length 'n' i.e. it could be 'jabra-a125'.
The '-' need to be converted to '+'.

If someone could either provide a solution or point me in the right direction i would be greatful.

g1smd

10:41 am on Aug 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code have you got so far?

adams station

10:59 am on Aug 18, 2008 (gmt 0)

10+ Year Member



well im not really sure how to match an unknown quantity of values, i was looking for some guidance before i started really as i wasnt sure how to progress.

jdMorgan

12:45 pm on Aug 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The length is of no real concern. What matters is being able to find the start and end of the string that must be copied from the requested URL to the internal filepath.

In the simplest case -rewriting the entire URL to the script filepath- you'd start with the character following the domain name and slash, and stop when you find a period -- or a period followed by ".htm".

In this regard, the length does not matter.

mod_rewrite is very inefficient for 'search and replace' since it has no internal looping or subroutine-calling capability; You would be better off to do this replacement within the script itself, if it is truly required. The other two solutions are to use multiple rules, each rule handling a number of character replacements, say 3, 2, and 1 for a total possible replacement count of six, or to re-start the entire mod_rewrite processing sequence after each replacement (See the [N] flag for RewriteRule). Neither of these solutions is very efficient, and the latter can bring a server to its knees if the .htaccess file is large.

We have several previous threads here on replacing underscores with hyphens and replacing spaces which demonstrate the efficiency problem; They can be found using the WebmasterWorld site search.

Jim