Forum Moderators: phranque

Message Too Old, No Replies

different rule, depending on the url

         

phoenix_fly

12:33 am on May 10, 2005 (gmt 0)

10+ Year Member



Hello guys,

Here´s the problem: I have two situations that I want to handle in different ways:

1) [thisvendor.mysite.com...]
-> [mysite.com...]

2) [thisvendor.mysite.com...]
-> [mysite.com...]

Here´s the rule I made:

RewriteEngine on
RewriteCond %{REQUEST_URI}!\.cgi
RewriteCond %{REQUEST_URI}!\.css
RewriteCond %{REQUEST_URI}!\.gif
RewriteCond %{REQUEST_URI}!\.jpg
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com
RewriteRule ^([^/^\\]+)[/\/]([^/^\\]+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1\&$2 [L]

It works fine for the #2 case, that is, when all the parameters are found. But, for case #1, I end up with a lost "&" in the rewritten url.

I´ve read the mod_rewrite docs several times, but couldn´t figure out how to write different rules for different kinds of url.

Hope you guys can help me, I´ve spent several hours on this, without success...

Thanks a lot!

phoenix_fly

phoenix_fly

2:27 am on May 10, 2005 (gmt 0)

10+ Year Member



Just to complement:

I did try to add this rule in the end, thinking that the urls that didn´t match the first pattern, would then match this one:

RewriteRule ^(.+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1 [L]

But then it does something unexpected: despite typing or not the "/products" or "/search/keyword=cases" or whatever, my page.cgi receives this as the 'section'value:

cgi-bin/page.cgi

I´m totally lost...

jdMorgan

4:32 am on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need two rules, one for 'products' and one for 'search'. Each rule must be preceeded by any RewriteConds you want to apply to it -- RewriteConds apply only to a single following RewriteRule. Since you may need to duplicate them, I'd suggest compressing your first four Conds to:

RewriteCond %{REQUEST_URI} !\.(cgi¦css¦gif¦jpg)$

Posting on this board modifies the "¦" pipe character -- replace the broken "¦" characters you see here with solid pipe characters from your keyboard before use.

Jim

phoenix_fly

1:57 pm on May 11, 2005 (gmt 0)

10+ Year Member



Hello Jim,

Thanks a lot for the post.

So, would it be this code:

RewriteEngine on
RewriteCond %{REQUEST_URI}!\.(cgi¦css¦gif¦jpg)$
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com
RewriteRule ^([^/^\\]+)[/\/]([^/^\\]+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1\&$2 [L]
RewriteCond %{REQUEST_URI}!\.(cgi¦css¦gif¦jpg)$
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com
RewriteRule ^(.+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1 [L]

?

Seems strange to me, but as you said the rewritecond only affects the next (the first?) rewriterule after it, I came to this.

Thanks

André

phoenix_fly

11:27 pm on May 11, 2005 (gmt 0)

10+ Year Member



Hello Jim,

The code above worked perfectly. By the way, there was a little typo in the first rule, so here´s the tested code:

RewriteEngine on
RewriteCond %{REQUEST_URI}!\.(cgi¦css¦gif¦jpg)$
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com
RewriteRule ^([^/^\\]+)/(.+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1\&$2 [L]
RewriteCond %{REQUEST_URI}!\.(cgi¦css¦gif¦jpg)$
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com
RewriteRule ^(.+) /home/mysite/www/cgi-bin/page.cgi?vendor=%1\&section=$1 [L]

But, as I had the beast domesticated, I am trying to be able to use "?" in the first RewriteRule regexp, instead of "/"

current: ^([^/^\\]+)/(.+)
desired: ^([^/^\\]+)\?(.+)

The reason is that the "?" is the normal behaviour for the html when I put the "http://www.vendor.mysite.com/search" as the "action" attribute in the FORM tag.

But even with the backslash it doesn´t work, I don´t understand why. Is there a special way to escape the "?" in mod_rewrite? Maybe use the hex equivalent?

Thanks

André

jd01

12:28 am on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread for the answer:

[webmasterworld.com...]

Justin