mod_rewrite メモ

f:id:tenman:20090621095548p:image


使い方:.htaccess - Rewrite

例えば、こんな感じで使う。

メンテナンス表示に使うとか、

http://web-tan.forum.impressrd.jp/e/2009/06/16/5880

#エラードキュメントを指定する
#httpから書くな!それは、リダイレクトに使えるかも
#http://sonic64.com/2004-09-08.html

ErrorDocument 503 /maintenance.html

<IfModule mod_rewrite.c>
  RewriteEngine On
#除外 メンテナンス表示ページ
  RewriteCond %{REQUEST_URI} !=/maintenance.html
#除外 管理者 IP
  RewriteCond %{REMOTE_ADDR} !=192.168.0.4
#その他のアクセスは、503リダイレクトして終了
  RewriteRule ^.*$ - [R=503,L]
</IfModule>

<IfModule mod_headers.c>
#終了予定時刻をヘッダに含める
  Header set Retry-After "Sun, 14 Jun 2009 6:00:00 GMT
</IfModule>

クエリを、ディレクトリやファイルのように見せるために、よく使われる。

frog cmsの例

#
# Setting rewrite rules
#

<IfModule mod_rewrite.c>
  RewriteEngine On
  # Set next line to your Frog root - if not in subdir, then just / 
  RewriteBase /
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Administration URL rewriting.
  RewriteRule ^admin(.*)$ admin/index.php?$1 [L,QSA]
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Main URL rewriting.
  RewriteRule ^(.*)$ index.php?PAGE=$1 [L,QSA]

</IfModule>

そもそも、URLの書き換えは必要なのか???

mod_rewriteを使うと、同じファイルに、いろんなアドレスでアクセスできてしまう。
見せかけてどうする?と問われれば、、、

Official Google Webmaster Central Blog: Dynamic URLs vs. static URLs

TOP