0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HTTPSかつWWWなしにアクセスを限定する(URLの書き換えとリダイレクト利用)

Posted at

文中の表記について

リンクになってしまうのを防ぐため、URLのプロトコル部分(http(s))の先頭の"h"は全角"H"に置き換えています。

やりたいこと

久しぶりに設定して頭がこんがらがったのでメモ

やりたいことは、あるサイトへのアクセスをURLを書き換えて(Rewrite)して、リダイレクトして以下に限定すること。
(個別の記載はあるけどまとまったのがない)

  • HTTPS限定
  • WWWなし

ドメインをyourdomain.jpとした場合、以下のようにしたい。

元のURL リダイレクト後
Http://www.youdomain.jp Https://youdomain.jp
Https://www.youdomain.jp Https://youdomain.jp
Http://youdomain.jp Https://youdomain.jp
Https://youdomain.jp Https://youdomain.jp
リダイレクトなし

設定

apache2.4httpd.confでの記述です。

ポイントは、★で囲んだhttps + wwwあり(https://www.yourdomain.jp)のアクセス。
ここで、この元URLに対応したSSL証明書を設定する必要があります。(wwwありとwwwなしはそれぞれ異なる証明書が必要であるため)
SSL証明書設定がなくても、リダイレクト先のSSL証明書を指定してもブラウザが警告発出してしまいます。

httpd.conf
<VirtualHost *:443>
  ServerName yourdomain.jp
  SSLEngine on
  SSL設定(ファイル参照)を記述
</VirtualHost>

<VirtualHost *:80>
  ServerName www.yourdomain.jp
  RewriteEngine on
  RewriteRule .* Https://yourdomain.jp%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
  ServerName www.yourdomain.jp
  RewriteEngine on
  RewriteRule .* Https://yourdomain.jp%{REQUEST_URI} [R=301,L]
  ★SSLEngine on★
  ★SSL設定(ファイル参照)を記述★
</VirtualHost>

<VirtualHost *:80>
  ServerName yourdomain.jp
  RewriteEngine On
  RewriteRule .* Https://yourdomain.jp%{REQUEST_URI} [R=301,L]
</VirtualHost>

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?