Alihkan url http ke url https menggunakan htaccess

Dec 27 2019

Saya menggunakan kode di bawah ini di file htaccess untuk mengarahkan www ke domain non www dan persyaratan seperti di bawah ini.

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]

Persyaratan :

  1. WWW ke Non WWW

    • http://www.example.com untuk https://example.com
    • https://www.example.com untuk https://example.com
  2. Non https url ke https Url

    • http://example.com untuk https://example.com
  3. Halaman lain selain url http ke url https

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

Hasil:

  1. bekerja dengan baik seperti yang diharapkan.

  2. & 3 tidak berfungsi dan tetap http url saat pengguna langsung masuk ke browser. Ini harus meningkatkan ke https url.

Kode berjalan di hosting bersama GoDaddy. Bantuan apa pun akan sangat dihargai. Jangan ragu untuk memberi tahu saya jika ada pertanyaan.

Jawaban

Wiimm Dec 27 2019 at 23:55

Saya menggunakan ini:

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]

Saya mengecualikan /.well-known/untuk memperbarui sertifikat mari enkripsi.

Di dalam <directory>atau untuk .htaccess:

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