リファレンスを読んでもいまいちピンと来ないmod_rewriteのRewriteRuleディレクティブのQSAフラグに関するメモ。
1. リファレンス原文
'
qsappend|QSA' (
query
string
append)
This flag forces the rewriting engine to append a query string part
in the substitution string to the existing one instead of replacing
it. Use this when you want to add more data to the query string via
a rewrite rule.
このフラグは、置換文字列の中にあるクエリ文字列部分を置き換えるのではなく、追加させる。これは、rewriteルールを通してクエリ文字列にデータを追加したい場合に使用する。
2. 実際の挙動
実際に挙動を見た方がわかりやすいのでQSAフラグあり/なしの時の動作をまとめる。
2.1 QSAフラグがないケース
RewriteRule ^/(.+)/pg/(.*)$ /$1/index.php?pg=$2 [L]
このルールは
http://www.bit-hive.com/april/pg/sample のようなURLを
http://www.bit-hive.com/april/index.php?pg=sample のように変換するものである。
QSAオプションがないと
http://www.bit-hive.com/april/pg/sample?code=1 のように入力URLにクエリ文字列が付加されていた場合でも
http://www.bit-hive.com/april/index.php?pg=sampleのようクエリ文字列が置き換えられてcode=1は消えてしまう。
2.2 QSAフラグがあるケース
上記のように入力URLにクエリ文字列が存在する場合は、QSAをいれて以下のようにしておけばよい。
RewriteRule ^/(.+)/pg/(.*)$ /$1/index.php?pg=$2 [QSA,L]
http://www.bit-hive.com/april/pg/sample?code=1 はhttp://www.bit-hive.com/april/index.php?pg=sample&code=1のように最後にクエリ文字列code=1が付加された形に変換される。
3. まとめ
QSAフラグがあると、入力URLに含まれていたクエリ文字列が変換後のURLにも付加されるようになる。