WordPressでサイトを運用していると.htaccessにURL書換えルールを追加したりするのですが、事情でhttpd.confに直接記述する必要ができたのでそのメモ書き。
テスト環境は下記です。
・CentOS release 6.5 (Final)
・Apache/2.2.15 (Unix)
当然ながらmod_rewriteを有効にしておく必要があります。
1 |
LoadModule rewrite_module modules/mod_rewrite.so |
mod_rewriteは機能が豊富ですべてを理解できません(^^;
今回は簡単なURLの書換えです。
http://www.rootlinks.net/Old_Link/
http://www.rootlinks.net/Old_Link/index.html
を
http://www.rootlinks.net/New_Link/index.html
に書換えます。
/etc/httpd/conf/httpd.confのDirectoryディレクティブにURL書換えルールを追加します。
1 2 3 4 5 |
<Directory "/var/www/html"> RewriteEngine on RewriteRule ^Old_Link/$ /New_Link/index.html [R=301,L] RewriteRule ^Old_Link/index\.html$ /New_Link/index.html [R=301,L] </Directory> |
今回はすでに以前のOld_Linkは削除して存在しないのでR=301(恒久的に移動)に設定しています。
書換えルールが正常に動作しない場合はログ出力を設定して確認するのがいいと思います。
ただしこのログ出力設定はhttpd.confかバーチャルホストでの記述のみ有効で.htaccessには記述できません。.htaccessに記述した場合はInternal Server Errorになります。
1 2 3 4 |
<IfModule mod_rewrite.c> RewriteLog /tmp/rewrite.log RewriteLogLevel 3 </IfModule> |
ログはレスポンスを低下させるので、正常に動作することが確認できたら、必ずコメントアウトして無効にします。
ログを出力するとこんな感じで確認することができます(一部抜粋)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
(3) [perdir /var/www/html/] add path info postfix: /var/www/html/Old_Link -> /var/www/html/Old_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/Old_Link/index.html -> Old_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'Old_Link/index.html' (3) [perdir /var/www/html/] add path info postfix: /var/www/html/Old_Link -> /var/www/html/Old_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/Old_Link/index.html -> Old_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'Old_Link/index.html' (2) [perdir /var/www/html/] rewrite 'Old_Link/index.html' -> '/New_Link/index.html' (2) [perdir /var/www/html/] explicitly forcing redirect with http://www.rootlinks.net/New_Link/index.html (1) [perdir /var/www/html/] escaping http://www.rootlinks.net/New_Link/index.html for redirect (1) [perdir /var/www/html/] redirect to http://www.rootlinks.net/New_Link/index.html [REDIRECT/301] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/New_Link/index.html -> New_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'New_Link/index.html' (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/New_Link/index.html -> New_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'New_Link/index.html' (1) [perdir /var/www/html/] pass through /var/www/html/New_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/favicon.ico -> favicon.ico (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'favicon.ico' (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/favicon.ico -> favicon.ico (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'favicon.ico' (1) [perdir /var/www/html/] pass through /var/www/html/favicon.ico |
ちなみに/var/www/html/.htaccessに記述した場合は下記になります。
1 2 3 4 |
# Rewrite Rule RewriteEngine on RewriteRule ^Old_Link/$ /New_Link/index.html [R=301,L] RewriteRule ^Old_Link/index\.html$ /New_Link/index.html [R=301,L] |
ログはこんな感じになりました(一部抜粋)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
(3) [perdir /var/www/html/] add path info postfix: /var/www/html/Old_Link -> /var/www/html/Old_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/Old_Link/index.html -> Old_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'Old_Link/index.html' (3) [perdir /var/www/html/] add path info postfix: /var/www/html/Old_Link -> /var/www/html/Old_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/Old_Link/index.html -> Old_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'Old_Link/index.html' (2) [perdir /var/www/html/] rewrite 'Old_Link/index.html' -> '/New_Link/index.html' (2) [perdir /var/www/html/] explicitly forcing redirect with http://www.rootlinks.net/New_Link/index.html (1) [perdir /var/www/html/] escaping http://www.rootlinks.net/New_Link/index.html for redirect (1) [perdir /var/www/html/] redirect to http://www.rootlinks.net/New_Link/index.html [REDIRECT/301] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/New_Link/index.html -> New_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'New_Link/index.html' (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/New_Link/index.html -> New_Link/index.html (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'New_Link/index.html' (1) [perdir /var/www/html/] pass through /var/www/html/New_Link/index.html (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/favicon.ico -> favicon.ico (3) [perdir /var/www/html/] applying pattern '^Old_Link/$' to uri 'favicon.ico' (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/favicon.ico -> favicon.ico (3) [perdir /var/www/html/] applying pattern '^Old_Link/index\\.html$' to uri 'favicon.ico' (1) [perdir /var/www/html/] pass through /var/www/html/favicon.ico |