Forum Moderators: phranque
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
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
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
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
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¶m2=$3$4 [PT,NC]
Unfortunately "¦" is still a part of the output
for instance: /search/¦/blah
results in
/search.do?param=¦¶m2=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
RewriteRule ^/search/([^\¦]*)/([^\¦]*)/?$ /search.do?param1=$1¶m2=$2 [PT,L]
Jim
Jim
^/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
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
^/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
# 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