4

Someone I'm working with committed a RewriteRule such as the following to SVN:

RewriteRule ^admin/ebay.*$ /yii.php/$1 [L]

I warned him that it may not work because there is no subgroup in the match that would correspond to the $1 backreference. It does work, and I'm perplexed. I'm pretty sure what he intended was either of the following:

RewriteRule ^admin/ebay.*$ /yii.php/$0 [L] # $0 is whole match

...or...

RewriteRule ^admin/ebay(.*)$ /yii.php/$1 [L] # $1 subgroup

Does Apache make an assumption about backreferences that I never knew about? Why does his RewriteRule (the top one) work?

  • From what I can tell URIs always start with `/` so `^admin` will never match anything. – Mark Wagner May 11 '11 at 18:29
  • There may be a configuration or circumstances in which that's true, but as far as I have ever seen, they do not begin with `/`. I just posted an answer to this particular issue. – Ezekiel Victor May 12 '11 at 17:04
  • In a _directory_ context (ie. inside a `` container) then the slash prefix is omitted (the same as if used in `.htaccess`). However, if used in a _server_ (or _virtualhost_) context then the slash prefix is required (since it matches the full URL-path). – MrWhite Apr 15 '18 at 00:38

2 Answers2

3

As it turns out, the $1 really doesn't do anything here. The following works just as well:

RewriteRule ^admin/ebay.*$ /yii.php [L]

(Notice no backreference at all in the rewrite part.)

This works because Yii is looking at $_SERVER['REQUEST_URI'] to figure out what the user intended. In fact Apache was just passing empty for the $1 backreference as expected.

So no server fault here. :)

1

Try setting RewriteLog and RewriteLogLevel. The log might give you a hint about where that value is coming from.

Jeff Snider
  • 3,272
  • 18
  • 17