HTTPS va WWW-ni majburlash uchun eng yaxshi usul nima, lekin localhost-ga ruxsat bering?

HTTPS va WWW-ni majburlash uchun eng yaxshi usul nima, lekin localhost-ga ruxsat bering? [O'zbek]

HTACCESS HTTPS va WWW-ni ishlatadi, lekin localhost-ga ruxsat beradi

HTTPS va WWW-ni bir necha sabablarga ko'ra majburlash juda muhim: ulanish xavfsizligini ko'rsatib, tashrif buyuruvchilaringiz ishonchini qozoning va agar siz www kabi quyi domendan foydalansangiz, tashrif buyuruvchilaringiz va hisobotlaringizni muvofiq holda, arzon veb-saytingizda. masalan, hosting.

Google Search Console kabi hisobot tizimlarida osonlikcha murakkablashishi mumkin, xuddi shu sayt va sahifalar uchun har xil ko'rinishga ega. Xavfsiz HTTPS ulanishi bilan, bittadan va har biri veb-subdomain bilan www, ikkinchisi esa ikkinchisisiz va hattoki kabusga aylanib, sizni doimiy ravishda hisobot berish uchun Google Analytics quyi domen filtrini o'rnatishga majbur qiladi.

HTACCESS kuchining HTTPS versiyasida va veb-subdomainda barcha tashrif buyuruvchilar xavfsizlikka yo'naltirilishini kafolatlashning ba'zi oson usullari mavjud, shuningdek, tashrif buyuruvchilarga ta'sir qilmasdan veb-saytni boshqa veb-saytga yo'naltirish uchun ham foydalanish mumkin. oqlangan usul.

Va buning ustiga va uni to'liq bajarish uchun, mahalliy subdomain veb-domensiz yoki HTTPS-ga yo'naltirilmasdan kirish mumkinligiga ishonch hosil qilish va kesish bilan tugaydigan sahifalar normal sahifaga yo'naltiriladi, oldini olish uchun. hisobotlardagi bir xil sahifalarni ikki marta hisoblash.

Keling, tahlillarning to'g'ri birlashtirilishini qiyinlashtiradigan ushbu ikki nusxadagi sahifalardan qutulamiz:

  • http://mywebsite.com/page

  • http://mywebsite.com/page/

  • https://mywebsite.com/page

  • https://mywebsite.com/page/

  • http://www.mywebsite.com/page

  • http://www.mywebsite.com/page/

  • https://www.mywebsite.com/page

  • https://www.mywebsite.com/page/

  • Bularning barchasi Apache serverlarida ishlatiladigan, odatda veb-saytning ildiz papkasida saqlanadigan yashirin fayl bo'lgan .htaccess faylini yangilash orqali amalga oshiriladi.

Slash bilan tugaydigan sahifani qayta yo'naltirish

Ulardan bittasi bilan tugaydigan URL sathidan olib tashlash va so'rovlarni standart sahifaga egri chiziqsiz yo'naltirish uchun URL-satrlarni boshi bilan va oxiri bilan tugaydigan URL-larga mos keling, u so'ralmagan katalog emasligiga ishonch hosil qiling va qayta yo'naltiring. barcha so'rovlar URIiga oxirigacha chiziqsiz.

Shuningdek, sahifani to'g'ri veb-manzilga yo'naltiring, u to'g'ri bo'lishi kerak - bu sizning barcha mumkin bo'lgan muammolaringizni, shu jumladan tugatish chizig'i, xavfsiz bo'lmagan so'rov yoki veb-subdomenni hal qiladi.

Bundan tashqari, biz katalogga bo'lgan har qanday so'rovning egri chiziq bilan tugashiga ishonch hosil qilish uchun qo'shimcha kodni olishimiz kerak, chunki oddiy veb-sayt kataloglarining URL manzillari ulardan biri bilan tugaydi.

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.mywebsite.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.mywebsite.com/$1/ [R=301,L]

HTTCESS kuchini HTTPS va WWW htaccess-ni majburlash

Endi tugallanadigan egri chiziq muammosi hal qilingandan so'ng, yo'qolgan HTTPS yoki yo'qolgan veb-subdomain haqida to'xtalib o'tamiz, htaccess http-ni http-ga http-ga yo'naltiradi va uni www.

Buni amalga oshirishning eng oson va eng zamonaviy usuli - bu ikkalasini ham aynan htaccess holatida aniqlash.

Bu osonlikcha veb-domen bilan boshlanmagan URL-larni yoki HTTPS-ni o'z ichiga olmaydigan har qanday so'rovlarni taqqoslash va veb-saytimizning to'g'ri URL-manzilini qayta yo'naltirish orqali amalga oshiriladi, bu ham xavfsiz ulanishni, ham veb-veb-domenni o'z ichiga oladi.

# HTACCESS force HTTPS and force WWW htaccess 
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

HTTPS va WWW-ni majburlash, ammo localhost-ga ruxsat berish

Endi bu juda yaxshi, lekin biz o'z veb-saytimizga XAMPP lokal serverida ishlash rejimida kirishga harakat qilganimizda nima sodir bo'ladi, lekin http: // localhost-ga bo'lgan barcha so'rovlarimiz endi https://www.mysite.com saytiga yo'naltiriladi - bizning ishlab chiqarish versiyamizmi?

URL so'rovining HTTPS va WWW versiyalarini amalga oshiradigan oldingi kodni qayta ishlatish, lekin uni localhost yoki bizning IP-manzilimizga (127.0.0.1) kiritilgan so'rovlarni chiqarib tashlash bilan boshlang.

Va bu, endi bizda barcha muammolarimizni hal qiladigan chiroyli kod mavjud!

# HTACCESS force HTTPS and force WWW htaccess 
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

HTTP-ni HTTPS htaccess-ga yo'naltiring, WWW-ni majburlang va tugash chizig'ini olib tashlang

Quyidagi barcha narsalarga erishish uchun quyida keltirilgan kodning to'liq qismini HTACCESS faylida ishlatish mumkin:

HTTP ni htacces-ga HTTPS-ga yo'naltirish,

WWW-ni htaccess-da majburlab oling,

URL-da tugash chizig'ini olib tashlang.

Albatta, veb-sayt URL-manzilini sizga murojaat qilganga o'zgartirishni unutmang.

U hatto veb-saytni boshqa veb-saytga yo'naltirish uchun ham ishlatilishi mumkin, bu sizning URL manzilingizni boshqa eng yaxshi arzon veb-hosting veb-saytingizga o'zgartiradi:

#--------------------------------------------------
# Force WWW htaccess : /
#--------------------------------------------------
#### Force HTTPS://WWW and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.mywebsite.com/%1 [R=301,L]

# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.mywebsite.com/$1/ [R=301,L]

# HTACCESS force HTTPS and force WWW htaccess
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

Shu kabi maqolalar


Izohlar (0)

Fikr qoldiring

Ezoic bilan reklama daromadini 50-250 foizga oshiring. Google Certified Publishing Partner.
Ezoic bilan reklama daromadini 50-250 foizga oshiring. Google Certified Publishing Partner.
 
Ezoic bilan reklama daromadini 50-250 foizga oshiring. Google Certified Publishing Partner.
Ezoic bilan reklama daromadini 50-250 foizga oshiring. Google Certified Publishing Partner.