LoginSignup
13
18

More than 5 years have passed since last update.

.htaccessでリダイレクト設定[www有無統一][http,https]

Posted at

どうも、はぐっです・ω・♪

「.htaccessでリダイレクトを楽に設定しちゃおう♪」

登場人物

IfModule

指定したモジュールが利用できる場合に中身を評価する。

mod_rewrite.c

リダイレクトとかURLの書き換えをしてくれる。
愛称:もどりらいと。

RewriteEngineディレクティブ

Rewrite機能のオン・オフを制御する。

RewriteCondディレクティブ

Rewrite機能のトリガーとなる条件を指定する。

RewriteRuleディレクティブ

書き換えるURLを指定する。

記述方法

www 有り・無し 統一方法

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hoge\.co\.jp
RewriteRule (.*) http://www.hoge.co.jp/$1 [R=301,L]
</IfModule>

はい、wwwきたー。

httpをhttps化

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

はい、httpsきたー。

ってな感じです。

で、実際の状況を考えて、
「wwwに統一、かつhttps化」
という状況の.htaccessの中身を載せちゃいます!

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hoge\.co\.jp
RewriteRule (.*) http://www.hoge.co.jp/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

覚えるにはめんどくさいけど、いつもやる作業をめもってみた。
件。

13
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
18