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 )
Amit KumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
Please see this bug report, and this work-around.

The problem is with the PATH_INFO portion of the object mod_rewrite is receiving.  When Apache receives a request for a non-existent file, it divides the requested URL into an "actual" URL which includes existing path information, and the PATH_INFO portion, which includes everything after and including the first part of the request not matchable to a physical path/file.  This information is normally useful at the application level, so it was always assumed it needed to be tacked on.

In your particular example, a look at the rewrite log shows how this works out (edited for brevity):
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] add path info postfix: /var/www/somedir/h/foo -> /var/www/somedir/h/foo/bar
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] strip per-dir prefix: /var/www/somedir/h/foo/bar -> foo/bar
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] applying pattern 'foo/bar' to uri 'foo/bar'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] rewrite 'foo/bar' -> '/tmp1/'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] explicitly forcing redirect with http://dev.somedir.com/tmp1/
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] add path info postfix: http://dev.somedir.com/tmp1/ -> http://dev.somedir.com/tmp1//bar
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] applying pattern 'foo/bar' to uri 'http://dev.somedir.com/tmp1//bar'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] add path info postfix: http://dev.somedir.com/tmp1/ -> http://dev.somedir.com/tmp1//bar
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] applying pattern '(.+)' to uri 'http://dev.somedir.com/tmp1//bar'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] rewrite 'http://dev.somedir.com/tmp1//bar' -> '/tmp3/'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] explicitly forcing redirect with http://dev.somedir.com/tmp3/
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] add path info postfix: http://dev.somedir.com/tmp3/ -> http://dev.somedir.com/tmp3//bar
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] applying pattern '(.+)' to uri 'http://dev.somedir.com/tmp3//bar'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] rewrite 'http://dev.somedir.com/tmp3//bar' -> '/tmp4/'
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] explicitly forcing redirect with http://dev.somedir.com/tmp4/
[Mon Jun 08 21:52:58] [perdir /var/www/somedir/h/] add path info postfix: http://dev.somedir.com/tmp4/ -> http://dev.somedir.com/tmp4//bar

Open in new window


Line 1 shows the first example of how mod_rewrite processes the PATH_INFO section - it appends it to the URL it receives.  In the first iteration, this is fine - it is actually processing the literal, original URL.  But after the first rule has rewritten, only the *URL* portion is replaced, leaving PATH_INFO='/bar'.  This PATH_INFO is carried over into the next rule.  You can see on line 6 where it is again appended to the URL (which is now, correctly, /tmp1/).

The fix is to tell mod_rewrite to discard this additional information.  This is done with the [DPI] flag on the RewriteRule directive.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.