Forum Moderators: phranque

Message Too Old, No Replies

Multiple conditions for dynamic url mod rewrite

Sometimes it's the simple things we need the most!

         

promoking

1:51 am on Jan 9, 2011 (gmt 0)

10+ Year Member



Is there a better way?

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} ^id=1$ [OR,NC]
RewriteCond %{QUERY_STRING} ^id=2$ [OR,NC]
RewriteCond %{QUERY_STRING} ^id=3$ [OR,NC]
RewriteCond %{QUERY_STRING} ^id=4$ [OR,NC]
RewriteCond %{QUERY_STRING} ^id=80$ [OR,NC]
RewriteCond %{QUERY_STRING} ^id=100$ [OR,NC]
RewriteRule path/filename\.asp TLD/Directory/? [R=301,L]

How's this:

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} ^id=([1-4,80,100]+)$ [NC]
RewriteRule path/filename\.asp TLD/Directory/? [R=301,L]

Thanks!

-Brian

g1smd

9:08 am on Jan 9, 2011 (gmt 0)

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



[1-4,80,100]+
allows the digits 1 to 4, a comma, the digits 8 and 0, to appear in any order and any amount of times, as long as the whole thing contains at least one character.

That is, "
00004,,,88,,1111
" would successfully match this pattern

You probably wanted
(1|2|3|4|80|100)
instead.


Additionally, the target URL for the redirect should state both the protocol and domain name in full within the rule.

jdMorgan

1:09 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... or retaining the alternate-character group:

RewriteCond %{QUERY_STRING} ^id=([1-4]|80|100)$ [NC]
RewriteRule ^local-URL-path/filename\.asp$ http://www.example.com/Directory/? [R=301,L]

Jim

promoking

7:52 pm on Jan 10, 2011 (gmt 0)

10+ Year Member



THANK YOU SO MUCH ! I've never seen the "|" separater used in any of the many examples and tutorials I've been pouring over and I guess I had a really poor understanding of the use of the comma... No wonder I was getting crazy results!

Can you please tell me if this would work?

RewriteCond %{QUERY_STRING} ^id=([10-40]|80|200)$ [NC]
RewriteRule ^local-URL-path/filename\.asp$ http://www.example.com/Directory1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=(90|[5-9]|2)$ [NC]
RewriteRule ^local-URL-path/filename\.asp$ http://www.example.com/Directory2/? [R=301,L]

The conditions in mod_rewrite 2 contain numbers that are out of sequence. Is that ok?

Also, does each mod_rewrite work independantly from each other so that conditions in one in no way affect any others when using [L]

Thanks!

-Brian

g1smd

8:07 pm on Jan 10, 2011 (gmt 0)

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



[10-40]
will match any single digit from 0 to 4. It might throw an error on the way, as the 0 is repeated, and the 1 is also within the 0-4 group.

[5-9]
will match any single digit 5 to 9.

If you want anything from 10 to 40, then
[1-3][0-9]|40
would do it. That might also simplify to
[1-3]\d|40
.

[5-9]|2
simplifies to
[25-9]
.

As well as the
[135]
and
[1-5]
notation, you also need to look at the
+
and
*
modifiers and the
{3}
notation.

If there are two conditions that might match, then by using the L flag on all of the rules only the first rule will get a chance to match.

promoking

9:56 pm on Jan 10, 2011 (gmt 0)

10+ Year Member



Wow... all I can say is thank you a million times.

These conditions are not real intuitive are they? I'm thinking that (36) = 36 but if I am getting this it actually = 3 or 6 or 36 or 63. But heck I probably even got this wrong!

g1smd

10:32 pm on Jan 10, 2011 (gmt 0)

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



(36) is 36.

[36] is a single 3 or 6.
[36]+ is at least one digit, any number of 3s and/or 6s in any order.
[36]{2} is exactly 2 digits, consisting only of 3s and/or 6s.

[3-6] is any single digit from 3 to 6.
and so on.

[^34] is "anything" as long as it is NOT a 3 or 4.

promoking

10:52 pm on Jan 10, 2011 (gmt 0)

10+ Year Member



Wow wow wow... Thanks a ton! You have no idea how much you've helped

jdMorgan

4:05 pm on Jan 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at the regular-expressions tutorial cited in our Apache Forum Charter. An hour spent with that document will save you days or weeks of wasted time and effort in the future. :)

Jim