Forum Topic: rewriterule without Last Flag

Forum: .htaccess Forum : Redirecting • Posted by amit kumar • Updated:

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]

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 though 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]

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 entered ( http://localhost/foo/bar ) then why it did not match on second line of same example and why it did not matched in second line of example 1 ?

remember all target pattern point to outside of the folder ( in parent, www, so that they can not hit back to the htaccess file again )