wiki:TipAndDoc/network/httpd/URLRedirect

Redirect with Location/Directory

  • RedirectディレクティブはLocationやDirectoryよりも先に評価されるようなので注意
    • 次のような設定では、「/jump/inner/hoge」にアクセスすると、認証をせずにいきなりexample.comにリダイレクトされてしまう
      <Location /jump>
      	AuthType Basic
      	AuthName "Authentication for jump"
      	AuthUserFile /path/to/passwd/htpasswd.file
      	Require valid-user
      </Location>
      RedirectMatch ^/jump/inner/(.*)$ https://example.com/inner/service/$1
      
    • RedirectMatchをLocation内に記述すればよい

Redirect and Rewrite URL

  • Pictnotes » RedirectMatch のリダイレクト先URLで、&(アンパサンド)がある場合
    • RedirectMatch のリダイレクト先のURLの中に、&(アンパサンド)がある場合、置換されてしまう。
      RedirectMatch ^/test/?  http://www.pictnotes.jp?param1=hoge&param2=fuga
      ↓
      RedirectMatch ^/test/? http://www.pictnotes.jp?param1=hoge/test
      
      RedirectMatch ^(/test/?)  http://www.pictnotes.jp?param1=hoge&param2=fuga
      ↓
      RedirectMatch ^(/test/?)  http://www.pictnotes.jp?param1=hogeparam2=fuga
      (&が消えている状態)
      
    • &を使うためには、エスケープしてあげればOK
      RedirectMatch ^(/test/?)  http://www.pictnotes.jp?param1=hoge\&param2=fuga
      & -> \&
      
  • クエリー付のリダイレクトは「mod_rewrite」を使うべし : Project MultiBurst
    • 以下のようにクエリーがついたURLを、すべて一つのURLにリダイレクトしようとすると、「Redirect」と「RedirectMatch」では無理なようです。
      Redirect /abc/ /def/index.php
      RedirectMatch /abc/(.*)$ /def/index.php
      
    • そもそも、「mod_alias」がクエリーまで扱っていないそうな。。なので、
      /abc/test.cgi?no=1&id=2
      
    • というリクエストが来ても、
      /def/index.php?no=1&id=2
      
    • という風にリダイレクトされてしまう。ということで、こういう場合は、「mod_rewrite」を使うとよい。
    • こんな感じ
      RewriteEngine On
      RewriteRule /abc/(.*)$ /def/index.php [R=301,L]
      
    • こうすることにより、クエリーはすべて破棄される。
Last modified 13 years ago Last modified on Dec 20, 2010 2:38:33 AM