ブログ運営に役立つ!リダイレクトに関する.htaccessファイルの設定のまとめ

ブログのドメインをwww有り、無しのどちらかに統一させたり、サブドメインを利用したり、新しいドメインに引っ越しをした時に役立つリダイレクトに関する.htaccssの設定を紹介します。

CMSでよく見かけるパラメータ(.php?username=namae&page=2)付きをすっきりしたURLにするのも便利です。

サイトのキャプチャ

Htaccess File Tutorial and Tips.

下記は各ポイントを意訳したものです。
※当ブログでの翻訳記事は、元サイト様に許可を得て翻訳しています。無断翻訳・まとめ記事の転載をしているあのブログの注意喚起を海外のブロガーから聞くのでご注意ください。

リダイレクトの前にやっておきたい.htaccessファイルの設定

.htaccessファイルの準備

「.htaccess」ファイルはテキストエディタで編集します。また、Apacheサーバーのphp.iniでmod_rewriteを有効にしておきます。

ディレクトリ一覧を表示させない

index.htmlがない場合、ディレクトリ一覧が表示されてしまうのを禁止します。

Options All -Indexes

エラーページの設定

404のエラーページに任意のファイルを指定します。

errorDocument 404 http://www.youwebsite.com/error.html

他のエラー時も設定できます。

errorDocument 400 http://www.youwebsite.com/error.html
errorDocument 401 http://www.youwebsite.com/error.html
errorDocument 500 http://www.youwebsite.com/error.html

リダイレクトに関する.htaccessファイルの設定

リダイレクト・リライトする時に必ず記述

リダイレクト・リライトを記述する時は、必ずRewriteEngineを「on」にします。

RewriteEngine on

www有りとwww無しに統一

「www無しのURL」を「www有りのURL」へ向け直し、www有りにします。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(yourwebsite\.com)(:80)?
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

www無しにする場合は、こんな感じに。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.yourwebsite\.com)(:80)?
RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]

サブドメインのリダイレクト

まずは、サブフォルダに接続してみます。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.yourwebsite\.com)(:80)?
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

サブドメインにリダイレクトする場合は、サブフォルダへのアクセスをサブドメインへ向け直します。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(subdomain\.yourwebsite\.com)(:80)?
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

新しいドメインに変更した時のリダイレクト

古いドメイン(abc.com)を新しいドメイン(xyz.com)へ向け直します。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]

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

URLを分かりやすいものに

英数字の羅列やパラメータなど分かりにくいURLをフレンドリーで分かりやすいものに変更します。

ビフォー
http://yourwebsite.com/profile.php?username=namae
アフター
http://yourwebsite.com/namae

パラメーターの値は「a-zA-Z0-9_-」として、ドメインの直下でアクセスできるようにします。

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
ビフォー
http://yourwebsite.com/messages.php?message_username=namae
アフターー
http://yourwebsite.com/messages/namae

サブフォルダ「messages」の直下でアクセスできるようにします。

RewriteEngine On
RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1
RewriteRule ^messages/([a-zA-Z0-9_-]+)/$ messages.php?message_username=$1
ビフォー
http://www.yourwebsite.com/friends.php?username=namae
アフターー
http://www.yourwebsite.com/friends/namae

サブフォルダ「friends」の直下でアクセスできるようにします。

RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1
ビフォー
http://www.yourwebsite.com/friends.php?username=namae&page=2
アフターー
http://www.yourwebsite.com/friends/namae/2

「friends」の配下ページは数字でアクセスできるようにします。

RewriteEngine On
RewriteEngine On
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2
ビフォー
http://www.yourwebsite.com/index.html
アフターー
http://www.yourwebsite.com/index

拡張子無しでアクセスできるようにします。

RewriteEngine On
RewriteRule ^([^/.]+)/?$ $1.html

sponsors

top of page

©2024 coliss