helen's blog

ずっとおもしろいことしてたいな。

cakePHPでapacheでリダイレクトさせようとしたけどだめだったよ

諦めたというオチです

やったこと
RewriteRule ^/helen https://www.google.co.jp/ [R=302,L]

こんな感じでapacheで /helen -> google にリダイレクトさせようとしてたのですがだめでした。

原因
AllowOverride All

これのせいで.htaccessのほうが強くなり、親に設定したリダイレクトが効いてませんでした。

オチ

apacheでリダイレクトできないけど

header('Location: https://www.google.co.jp/');

はしたくなかったのでapp/Config/routes.phpに下記設定。
ref) ルーティング

// 内部遷移
Router::redirect(
    '/helen/index/', 
    array('controller' => 'hoge', 'action' => 'fuga'), 
    array('status' => 301)
);

// もちろん/helen/だけでも外部サイトでも行ける
Router::redirect(
    '/helen/*', 
    'https://www.google.co.jp/', 
    array('status' => 301)
);
おまけ

cakePHPに入っているラスボスはこんな.htaccessです
app/.htaccess at master · cakephp/app · GitHub

ログを見るとこんな感じ。多いのでちょっと整形しました。
DocumentRootは/home/vagrant/webRoot/です

[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/helen -> helen
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'helen'
[rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-d' => matched
[rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-f' => matched
[rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] rewrite 'helen' -> 'index.php'
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] add per-dir prefix: index.php -> /home/vagrant/webRoot/index.php
[rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] strip document_root prefix: /home/vagrant/webRoot/index.php -> /index.php
[rid#7f8af0cdc7f8/initial] (1) [perdir /home/vagrant/webRoot/] internal redirect with /index.php [INTERNAL REDIRECT]
[rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/index.php -> index.php
[rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'index.php'
[rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-d' => matched
[rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-f' => not-matched
[rid#7f8af0cee110/initial/redir#1] (1) [perdir /home/vagrant/webRoot/] pass through /home/vagrant/webRoot/index.php

分解すると

# /helenのアクセスのDocumentRootをはずす
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/helen -> helen

# RewriteRuleの ^ をhelenに適応できるか?
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'helen'

# RewriteCondそれぞれ確認
[rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-d' => matched
[rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-f' => matched

# matchedなのでhelen -> index.phpへ書き換え
[rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] rewrite 'helen' -> 'index.php'

# DocumentRootをつける
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] add per-dir prefix: index.php -> /home/vagrant/webRoot/index.php

# はずす
[rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] strip document_root prefix: /home/vagrant/webRoot/index.php -> /index.php

# /index.phpへリダイレクト
[rid#7f8af0cdc7f8/initial] (1) [perdir /home/vagrant/webRoot/] internal redirect with /index.php [INTERNAL REDIRECT]

ここでridが変わる

# またDocumentRootを外す
[rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/index.php -> index.php

# 条件にマッチするか確認するよ
[rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'index.php'

# 条件だよ
[rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-d' => matched
[rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-f' => not-matched

# これを表示するよ
[rid#7f8af0cee110/initial/redir#1] (1) [perdir /home/vagrant/webRoot/] pass through /home/vagrant/webRoot/index.php

.htaccessしか効いてないのに親をいじってapache再起動しまくってたのが虚しい