LoginSignup
0
0

More than 5 years have passed since last update.

Apacheのmod_rewriteモジュールで旧ディレクトリへのアクセスをリダイレクトする

Posted at

状況

移設作業に伴い、www.example.com 以下でアクセスするディレクトリが変わる。
旧:www.example.com/old-uri
新:www.example.com/new-uri

old-uriを廃止し、「旧」へのアクセスを「新」へリダイレクトしたい。
new-uriは既に稼働していて、いくつかRewriteRuleが設定されている。

要点

  1. すでに設定されているRewriteCond の前に RewriteRule を追加
  2. R=301による301リダイレクト(永久転送)

1.については、
http://oxynotes.com/?p=7392

注意点としてRewriteCondの直後に来たRewriteRuleしか有効にならない。2行続けてRewriteRuleを記述しても下の行にはRewriteCondで一致した項目は渡されない。
もし同じRewriteCondを適応するには以下のように二回書くか、フラグでルールの継続を明示する必要がある。

【before】の2,3行目のつながりを崩さないように、RewriteEngineの直後に設定を追加した。

before

before
RewriteEngine   ON
RewriteCond     $1                         !\.someextension$
RewriteRule    ^/new-uri/([0-9a-zA-Z]*);param=(.*)$ /new-uri/$1.someextension;param=$2 [PT,L,QSA]
RewriteRule    ^/new-uri/([0-9a-zA-Z]*)$    /new-uri/$1.someextension [PT,L,QSA]

#--------------------------------------------------------------------------------------------
# old-uri
#--------------------------------------------------------------------------------------------

<Location /old-uri>
    Balancerを呼ぶ設定とか
</Location>

#------------------------------------------------------------------------------
# new-uri
#------------------------------------------------------------------------------

<Location /new-uri>
    Balancerを呼ぶ設定とか
</Location>

after

RewriteEngine   ON
RewriteRule     ^/old-uri/(.*)$               /new-uri/$1                [R=301,QSA]
RewriteCond     $1                         !\.someextension$
RewriteRule    ^/new-uri/([0-9a-zA-Z]*);param=(.*)$ /new-uri/$1.someextension;param=$2 [PT,L,QSA]
RewriteRule    ^/new-uri/([0-9a-zA-Z]*)$    /new-uri/$1.someextension [PT,L,QSA]

#------------------------------------------------------------------------------
# old-uri
#------------------------------------------------------------------------------

#<Location /old-uri>
#    Balancerを呼ぶ設定とか
#</Location>

#------------------------------------------------------------------------------
# new-uri
#------------------------------------------------------------------------------

<Location /new-uri>
    Balancerを呼ぶ設定とか
</Location>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0