I don't want to blame RewriteRule, but so it looks to me.
Minimal example. In the real code, the first Rule does some prepending, so it has some meaning, as opposed to this.
RewriteRule ^(.*)$ $1
RewriteCond %{REQUEST_URI} ^/pre
RewriteRule ^(.*)$ /post=$1 [R=301,L]
When submitted URL is example.com/pre/sub
, I am getting 301 to example.com/post=pre/sub/sub
. I have no idea where that second sub is coming from.
The way I think this should work. First rule $1
is pre/sub
and so it rewrites pre/sub
to pre/sub
. The cond checks whether REQUEST_URI starts with /pre and prevents looping and the last rule matches everything so $1
is pre/sub
and it appends it creating /post=pre/sub
, to which it is then redirected. But the browser gets /post=/pre/sub/sub
.
Some testing I have done:
Submitting only example.com/pre
results in expected /post=pre
;
When I modify the example to
RewriteRule ^(.*)$ -$1-
RewriteCond %{REQUEST_URI} ^/pre
RewriteRule ^(.*)$ /post=&$1& [R=301,L]
The resulting redirect is to /post=&-pre/sub-/sub&
meaning it is appended "after" the first rule, but "before" the second one. But where and how?