.htaccess | memo-memo - Part 2 Skip to content

memo-memo

web制作に便利そうな情報をメモるブログ

Archive

Tag: .htaccess

.htaccessを使って、リダイレクトさせる。

wwwありに統一させるには、以下を、.htaccessに記述

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.com
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]

wwwなしに統一させるには、以下を、.htaccessに記述

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.abc\.com
RewriteRule ^(.*)$ http://abc.com/$1 [R=301,L]

赤字の部分(この場合、abccom)は、その時に使うドメイン名に直して記述する。

.htaccessに、以下を記入する。

例)http://www.abc/123/

とアクセスしたら、

http://www.abc/index.php?id=123

こんな感じで、123をindex.phpへ、GETで渡したいとき。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule /(.+?)/ /index.php?id=$1

意味は、
RewriteEngine onは、mod_rewriteさせるためのスイッチをonにしてる。
RewriteCond %{REQUEST_FILENAME}!-fは、php(CGI)より先に、FTPを読みに行っている。この場合、www.abc/123/に、index.htmlが存在した場合、index.phpは、働かない。要は、静的なhtmlファイルがある場合は、そちらを優先させる。(省略可能)

RewriteRule /(.+?)/ /index.php?id=$1は、http://www.abc/xxx/の赤字に入る文字(数字かアルファベット)を、index.phpへ、GETで渡す。