うまいぼうぶろぐ

linuxとhttpdとperlのメモ

mod_rewrite でBasic/Digest認証ユーザの名前を変数に利用する

Other things you should be aware of: の5番。

%{LA-U:variable} can be used for look-aheads which perform an internal (URL-based) sub-request to determine the final value of variable. This can be used to access variable for rewriting which is not available at the current stage, but will be set in a later phase.
For instance, to rewrite according to the REMOTE_USER variable from within the per-server context (httpd.conf file) you must use %{LA-U:REMOTE_USER} - this variable is set by the authorization phases, which come after the URL translation phase (during which mod_rewrite operates).

On the other hand, because mod_rewrite implements its per-directory context (.htaccess file) via the Fixup phase of the API and because the authorization phases come before this phase, you just can use %{REMOTE_USER} in that context.

httpd.conf でvitualhost内でserver全体に設定する場合は、認証の後にmod_rewriteが動くように%{REMOTE_USER}ではなくて、%{LA-U:REMOTE_USER} としないとダメみたい。(look ahead url-based の略?)

RewriteEngine On
RewriteCond   %{REQUEST_URI} ^/foo/$
RewriteRule   ^/foo/ /foo/%{LA-U:REMOTE_USER}/ [R,L]


一方、httpd.conf でもdirectoryやlocation内に書く場合や、.htaccess で各ディレクトリごとに設定する場合は 先に認証がかかるから普通に%{REMOTE_USER} で良い?

<Directory /path/to/htdocs/foo>
  RewriteEngine On
  RewriteBase   /foo/
  RewriteCond   %{REQUEST_URI} ^/foo/$
  RewriteRule   ^/? /foo/%{REMOTE_USER}/ [R,L]
</Directory>