htaccessを使用してhttpURLをhttpsURLにリダイレクトします

Dec 27 2019

htaccessファイルで以下のコードを使用してwwwをwww以外のドメインにリダイレクトし、以下の要件を満たしています。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$ RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]


RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]

要件

  1. WWWから非WWWへ

    • http://www.example.com に https://example.com
    • https://www.example.com に https://example.com
  2. httpsURLからhttpsURLへの非httpsURL

    • http://example.com に https://example.com
  3. httpurlからhttpsurl以外のその他のページ

    • http://example.com/blog/7-best-places-to-visit に https://example.com/blog/7-best-places-to-visit

結果:

  1. 期待どおりに正常に動作しています。

  2. &3は機能せず、ユーザーがブラウザに直接入力してもhttpurlのままです。httpsurlにアップグレードする必要があります。

コードはgodaddy共有ホスティングで実行されています。どんな助けでも大歓迎です。ご不明な点がございましたら、お気軽にお問い合わせください。

回答

Wiimm Dec 27 2019 at 23:55

私はこれを使用します:

RewriteEngine On
# the following line is only needed, if HTTP and HTTPS share the same config
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^/.well-known/
RewriteRule .* https://wiimmfi.de$0 [R=301,L]

/.well-known/Let'sEncrypt証明書を更新するために除外しました。

内部<directory>または.htaccessのために:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^.well-known/
RewriteRule .* https://wiimmfi.de/$0 [R=301,L]