Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond help needed

         

madeonmoon

1:54 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Hello all,

I am having a hard time writing the following rule. I'd like to combine part 1 and part 2 in as fewer lines of config as possible.

Part 1 (easy)
If user requests /search/<keywords>/<category>/<sortby>/<sorttype> where keywords is an actual string, the following should be invoked: /search.php?keyword=<keywords>&category=<category>&sortby=<sortby>&sorttype=<sorttype>

Part 2 (not-so-easy)
However, if the user requests /search/<keywords>/<category>/<sortby>/<sorttype> with "¦" values in place of one or more params (meaning no filtering for that param is needed), I'd like to exclude such param(s) from the search criteria (i.e. /search.php?keyword=seo&category=webmaster when the original search contained /search/seo/webmaster/¦/¦ -- only <keywords> and <category> are supplied )

I thinks it is a combination of RewriteCond and RewriteRule that needs to be applied but have no experience of dealing with the former.

Any help will be greatly appreciated!
James

jdMorgan

2:31 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I don't think you'd have to use RewriteCond... although I don't think of RewriteCond as particularly daunting, and encourage you to experiment with it.

Addressing your 'missing' variables problem: In simplest terms, all you really need to do is to look for the pattern
"/ <not a pipe character> /"
when pattern-matching to extract the variables from the requested URL.

In regex, the pattern fragment would be "/[^¦/]+/" -- That is, slash, then one or more characters not a pipe or a slash, then another slash.

If you haven't read them yet, there are several useful references cited in our forum charter [webmasterworld.com], as well as information on how to get the most from this forum.

Jim

madeonmoon

2:54 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Jim,

Thanks for your reply.

The problem with matching for params whose values are other than "¦" (which I thought of doing, btw), is that I'd have too many combinations of possible RewriteRule lines to include in my VirtualHost configuration to accomodate for any and all missing search params.

I was hoping to accomplish the same with a catch-all config consisting of a couple of lines only.

Any other ideas you can think of?

Thanks again,
James

jdMorgan

3:14 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two questions which may may this easier:

1) Are the parameters in the static URL alwyas in the same order?

2) Will your script accept "parameter1=value1&parameter2=&parameter3=value3" ?

That is, will it care if the name is present, but the value is blank?

If the script will work with a blank parameter value and the parameter order is fixed, then only one RewriteRule wouid be required.

Jim

madeonmoon

3:18 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Jim,

The answer to both questions is Yes.

So, the main challenge for me right now is to convert "¦" on the static url to "" on the resulting querystring. I am not sure how this can be done.

Thanks,
James

jdMorgan

5:06 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The documents cited in our charter are a good place to start...

If you create a back-reference, and the subpattern for that back-reference does not match, then the back-reference will be blank. This makes your code very easy to implement -- in one line.

Post your best effort, and we can help.

Jim

tehtreag

5:35 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



I use the following technique to log html enabled readers of my monthly newsletter. In each newsletter I include a HTML message that contains a image fetched from my server. Each message has a unique-id for each user.

Here is an example of a URL sent to the users... [mailloger.mydomain.domain...]

I use mod_rewrite to change this to /logemailuser.php?userid=UNIQ-9182312-USER-1231-ID&image=actualimage.gif

The script logemailuser.php stores a record in the database and sends the user the image actualimage.gif.

This is done with the following RewriteRule.


RewriteEngine On
RewriteRule ^/triggerurl/([^/]+)/([^/]+)/([^/]+) /$1.php?userid=$2&image=$3
[L]

A similar rule should handle your vertical bars/pipes and you can handle them appropriately in your server side script. I use triggerurl to trigger the use of this rule. I use it a few other cases. The rule uses the second "directory" as the script name $1, the third and fourth as arguments userid $2 and image $3.

I could use more generic names like arg1 and arg2, but so far the use of userid has been consistent and only image has been used for otherthan images.

Here is another example...
[mailloger.mydomain.domain...]

gets rewritten as,
/annoyuser.php?userid=UNIQ-9182312-USER-1231-ID&image=sendemail

In this case, the user gets sent another email in response to reading the message with an HTML enabled mail reader that contains the above URL.

I hope this helps. And of course I don't use names such as logemailusers, and maillogger, or even triggerurl. Substitute as needed. And remember YMMV.

-teh

madeonmoon

6:31 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Jim,

I understand your suggestion (trying to back-reference non-existing pattern will result in empty string) and this is what i came up with (assuming i only have 2 possible search params)

^/search/([\¦]?)([a-z]*)/([\¦]?)([a-z]*)/?$ /search.do?param1=$1$2&param2=$3$4 [PT,NC]

Unfortunately "¦" is still a part of the output

for instance: /search/¦/blah

results in

/search.do?param=¦&param2=blah

tehtreag, thanks for your help! I'll read your post again to see if I can utilize it (so far I am not sure how)

James

madeonmoon

6:40 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Oops, stupid me. The following does the trick:

^/search/([\¦]?)([a-z]*)/([\¦]?)([a-z]*)/?$ /search.do?param1=$2&param2=$4 [PT,NC]

/search/¦/blah
results in
/search.do?param=&param2=blah

Is this how you'd do it?

Best,
James

jdMorgan

7:07 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm seeing some strange character-encoding problems with your post, probably because you're not using ISO-8859, but the following code should be more efficient:

RewriteRule ^/search/([^\¦]*)/([^\¦]*)/?$ /search.do?param1=$1&param2=$2 [PT,L]

Each subpattern accepts zero or more characters not equal to "/" or "¦" and creates a back-reference containing those characters or blank if there are none.

Jim

madeonmoon

7:24 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Jim,

Thanks for this but your version does not seem to work.. I can't give more details as i have to run out

James

jdMorgan

8:45 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, that's because I copied and pasted without thinking... slashes, not backslashes...

RewriteRule ^/search/([^/¦]*)/([^/¦]*)/?$ /search.do?param1=$1&param2=$2 [PT,L]

Jim

madeonmoon

12:58 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Jim,

Thanks again. The latest revision of yours results in: /search.do?param1=¦&param2=stuff when /search/¦/stuff is accessed. So the pipe is still being preserved which is what I was trying to avoid.

jdMorgan

6:07 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that should not be happening, since pipes and slashes are clearly excluded from the back-referenced patterns. However, posting on this board modifies pipe characters so make sure you re-type those if you cut-and-pasted the code. That is, replace all broken pipe "¦" characters above with solid pipes from your keyboard (Shift-\ on U.S. 101-key keyboards).

Jim

madeonmoon

6:41 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Jim,

Just to make sure we are on the same page" is there a pipe (solid, not broken) in your rewrite directive?

RewriteRule ^/search/([^/�]*)/([^/�]*)/?

I don't see any (I see ^, forward slash, and a question mark in the square brackets).

jdMorgan

8:03 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's apparently a character-set incompatibility between your browser and this forum. When I look at your posted code, I see 'strange' characters as well. The regex should be:

^/search/([^/¦]*)/([^/¦]*)/?$

That is:

carat slash "search" slash left-parenthese left-square-bracket carat slash solid-pipe right-square-bracket asterisk right-parenthese slash left-parenthese left-square-bracket carat slash solid-pipe right-square-bracket plus-sign right-parenthese slash question-mark dollar-sign

:)

Jim

madeonmoon

2:26 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



Jim,

I really appreciate the time you took to "transcribe" the directive :)

I typed it in manually but it only works for string params, not pipes. The latter does not cause the RewriteRule to apply. I don't expect you to spend any more time on this; I'll play with it myself ;)

All the very best
James

ChadSEO

2:36 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



I think I noticed what may be causing the problem. There is no match-a-pipe-if-it-exists portion to the regex, here's an updated version that should fix that:

^/search/([^/¦]*)¦?/([^/¦]*)¦?/?$

That is:

carat slash "search" slash left-parenthese left-square-bracket carat slash solid-pipe right-square-bracket asterisk right-parenthese solid-pipe question-mark slash left-parenthese left-square-bracket carat slash solid-pipe right-square-bracket asterisk right-parenthese solid-pipe question-mark slash question-mark dollar-sign

jdMorgan

9:43 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming that *any* parameter might be present or might be omitted (equal to a pipe), you have to construct the query piecemeal, something like this:

# Remove trailing slash if present, clear any query string
RewriteRule ^/search/([^/]+/[^/]+/[^/]+/[^/]+)/?$ /$1?
#
# Append keyword to query string if present
RewriteRule ^/search/(([^/¦]+)/[^/]+/[^/]+/[^/]+) /$1?keyword=$2 [QSA]
#
# Append category to query string if present
RewriteRule ^/search/([^/]+/([^/¦]+)/[^/]+/[^/]+) /$1?category=$2 [QSA]
#
# Append sortby to query string if present
RewriteRule ^/search/([^/]+/[^/]+/([^/¦]+)/[^/]+) /$1?sortby=$2 [QSA]
#
# Append sorttype to query string if present
RewriteRule ^/search/([^/]+/[^/]+/[^/]+/([^/¦]+)) /$1?sorttype=$2 [QSA]
#
# Rewrite URL to search.php (query will be appended)
RewriteRule ^/search/[^/]+/[^/]+/[^/]+/[^/]+ /search.php [L]

All occurances of [^/¦]] are "left-square-bracket carat slash solid-pipe right-square-bracket"

Jim

madeonmoon

9:49 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



ChadSEO,

Thanks a lot for your help! I dilligently typed it in but no luck.

Jim,

Thanks again. This make me wonder whether the way that I came up with originally (which works) would be more efficient since all is done within one rewrite statement.

jdMorgan

12:46 am on Aug 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no othr way to do this that is more efficient. Again, if *any* parameter may be missing, you have to build the query one parameter at a time. You could also do it with 16 individual rules, one for each combination of all four parameters being defined or undefined.

Jim

madeonmoon

6:02 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Jim,

Thanks a bunch again. I *really* appreciate your help with this ;)

James