Link to home
Start Free TrialLog in
Avatar of Amit Kumar
Amit Kumar

asked on

A bunch of RewriteRule without Last flag

I am trying to understand behaviour a bunch of rewriterule rules without Last flag
Consider my htaccess file location is http://localhost/h/ ( Applications/AMPPS/www/h )

Example 1:  
RewriteRule anchor/(.+) /hello [R,ENV=lang:hi]
RewriteRule anchor /anchor/guess [R]
RewriteRule /hello /yes [R]

Open in new window

If I enter http://localhost/h/anchor/text

then what I think happen is

1. First line RewriteRule anchor/(.+) /hello [R,ENV=lang:hi] 's pattern "anchor/(.+)" matches and so it redirects to http://localhost/hello , but as there is no L flag, redirection to http://localhost/hello is on hold ,thus it goes below
2. Line second's pattern "anchor" does not match with new http://localhost/hello so this is skipped
3. third line's pattern /hello matches with http://localhost/hello and it finally redirects to http://localhost/yes 

Things seems as per I thought untill I see example below
Example 2:
RewriteRule foo/bar /tmp1/ [R]
RewriteRule foo/bar /tmp2/ [R]
RewriteRule (.+) /tmp3/ [R]
RewriteRule (.+) /tmp4/ [R]
RewriteRule hello /tmp6/ [R]
RewriteRule bar /tmp7/ [R]
RewriteRule hello /tmp8/ [R]
RewriteRule tmp7/ /tmp5/ [R]

Open in new window

same htaccess file location, I hit http://localhost/h/foo/bar ,
I thought this happens
1. first line's pattern "foo/bar" matches with url , so it redirects to http://localhost/tmp1 , but as there is no L , its on hold , it goes below

2. second line's pattern "foo/bar" does not match with http://localhost/tmp1/ , so its skipped, ( if I remove all lines except first two , I see final redirects to http://localhost/tmp1/)

3. third line's pattern match with http://localhost/tmp1/ and turned to http://localhost/tmp3/

4. fourth line's pattern matches, redirect to http://localhost/tmp4/

5. fifth line's "hello" not matched redirects still is http://localhost/tmp4/

6. and now which thing spin my mind for last 3 hrs is sixth's line "bar" matches .. and redirects to http://localhost/tmp7/ ( remove last 2 lines to confirm ) how ?

7. seventh's does not match as expected

8. eighth line tmp7/ matches with http://localhost/tmp7/ and then finally redirects to http://localhost/tmp5/

now question is why 6'th line's "bar" matches and if it can match with oldest url too which was entered like http://localhost/foo/bar , then why it did not match on second line of same example  or why it did not matched in second line of example 1 ?

remember all target pattern point to outside of the folder  ( in parent www folder, so that they can not hit back to the htaccess file again )
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial