Forum Moderators: phranque

Message Too Old, No Replies

help needed with a RewriteRule

how to define this rewriterule

         

phparion

7:42 am on Jan 10, 2007 (gmt 0)

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



Hi

I have websites URLs like,

mydomain.com/firstvar/
mydomain.com/firstvar/secondvar/

I am writing .htaccess for it and have written the following two rules

RewriteBase
RewriteRule ^/([^.]+)/$ page1.php?var1=$1 [T=application/x-httpd-php]
RewriteRule ^/([^.]+)/([^.]+)/$ page2.php?var1=$1&var2=$2 [T=application/x-httpd-php]

now, what I am expecting is that when the first URL with one var is called it should call the page1.php by sending one variable and when the second URL is called it must be redirected to the page2.php with two variables.

But the htaccess is not working in the desired mode. No matter which URL i call it always redirect to the page1.php

I was wondering if anyone can help to make the htaccess work in the desired way.

thanks in advance

phranque

8:44 am on Jan 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would try switching the order of the RewriteRules.
put the more specific rule (or regular expression) first.

phparion

10:37 am on Jan 10, 2007 (gmt 0)

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



thanks for your reply though I have already fixed it :)

phranque

11:56 am on Jan 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



how did you fix it?

jdMorgan

3:14 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The negative-match patterns were incorrect, and should have been:

RewriteRule ^/([^/]+)/$ page1.php?var1=$1 [T=application/x-httpd-php,L]
RewriteRule ^/([^/]+)/([^/]+)/$ page2.php?var1=$1&var2=$2 [T=application/x-httpd-php,L]

Note also the addition of the [L] flag -- highly recommended *unless* you have a good reason not to use it.

Jim

phparion

10:58 am on Jan 12, 2007 (gmt 0)

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



i reversed the order of rules exactly as you has suggested :)

Morgan: Rules are working for me not sure what are you pointing to?

jdMorgan

3:23 pm on Jan 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The subpattern "([^.]+)" means "match one or more characters not equal to a period." The code will be much more efficient if you use "([^/]+)", meaning "match one or more characters not equal to a slash," or equivalently and more appropriate to the goal here, "match all characters up to the next slash."

As it is, the regular-expressions pattern-matcher will have to run several matching attempts, trying to find a "best-fit" of the requested URL-path to the pattern, instead of being able to match the request to the pattern (or reject it) in a single left-to-right pass. The number of retries will be equal to the number of characters in the final slash-delimited part of the path, minus one.

Jim