よく使われる301リダイレクト4つを.htaccessで設定する方法(コピペOKなサンプルコード)をご紹介。
以下4つの301リダイレクトに対応したい方の助けになれるでしょう。
- httpからのアクセスをhttpsにリダイレクト
- httpsからのアクセスでwwwつきの場合、www無しにリダイレクト
- index.phpにアクセスされたときに、トップページへリダイレクト
- index.htmlにアクセスされたときに、トップページへリダイレクト
コピペOKなサンプルコードは下記のものとなります。
○○○○〜の箇所をご自身のドメインに合わせて使ってください。
▼▼▼
htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# httpからのアクセスをhttpsにリダイレクト
RewriteCond %{HTTPS} off
RewriteRule ^(.*$) https://〇〇〇〇.com/$1 [R=301,L]
# httpsからのアクセスでwwwつきの場合、www無しにリダイレクト
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.〇〇〇〇.com$
RewriteRule ^(.*)$ https://〇〇〇〇.com/$1 [R=301,L]
# index.phpにアクセスされたときに、トップページへリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://〇〇〇〇.com/$1 [R=301,L]
# index.htmlにアクセスされたときに、トップページへリダイレクト
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://〇〇〇〇.com/$1 [R=301,L]
</IfModule>
次はこの記事!






