Ka'idodin .htaccess: Yadda ake amfani da & misalan

Mataki na ashirin da ya rubuta ta:
  • Jagoran Gida
  • Updated: Jul 29, 2019

Menene fayil din .htaccess?

Fayil .htaccess shine Apache HTTP Server (wanda ake kira "Apache" kawai). Fayil din yana da iko sosai kuma za'a iya amfani dasu don taimakawa wajen sarrafa sassan shafukan yanar gizo waɗanda Apache ke aiki. Wannan ya haɗa da abubuwa kamar sarrafawa da saukewa, tsaro da kariyar.

Ina ne fayil .htaccess?

Ya kamata a sami fayil daya .htaccess a cikin babban fayil na tushen yanar gizonku - babban fayil (yawanci / public_html ko / www) wanda ke riƙe da abun ciki na shafin yanar gizonku.

Za ka iya samun fayil fiye da ɗaya .htaccess a asusunka na asusunka, amma kowane shugabanci ko babban fayil zai iya samun ɗaya. Alal misali, zaka iya samun fayiloli daban-daban na .htaccess a cikin babban fayil ɗinka kuma wani a cikin babban fayil. Wannan yana ba ka damar saita nau'in uwar garke daban-daban bisa tsarin ginin.

Fayil .htaccess yana da iko amma ana iya isa da kuma gyara idan ba a kare shi daidai ba. Tabbatar cewa kayi matakai don hana samun dama ga wannan fayil.

Ba zan iya samun fayil na .htaccess na ba

Da farko .htaccess shine fayil Apache, wannan yana nufin za ka iya samo shi a cikin uwar garken Apache. * Tsayawa nema idan mai masaukinka yana gudana a kan wani software daban-daban na yanar gizo (watau Microsoft IIS ko NGINX).

Sunan fayilolin da suka fara tare da ɗigon shafukan da aka adana da yawa. Wannan yana nufin ba su da yawanci a bayyane.

Don duba wannan fayil, danna kawai "Nuna Fayilolin Hoto" a cikin FTP abokin ciniki ko Manajan Mai sarrafa fayil (duba hoton da ke ƙasa).

Nemi fayil .htaccess - Yadda za a nuna fayilolin ɓoye a cPanel mai sarrafa fayil
Misali - Nuna fayilolinku na ɓoye ta hanyar samun wannan zaɓi a cPanel File Manager.

Kuna iya jin cewa fayil na .htaccess yana da damuwa tun lokacin da yake buƙatar coding amma la'akari da mai amfani wanda yake buƙatar kafa irin ƙwayar uwar garke a fadin shafukan yanar gizo. Duk mutumin da yake buƙata ya yi shi ne duplicate fayil .htaccess.

.htaccess fayiloli ne fayilolin sanyi na uwar garke kuma tun da sunyi magana da halayyar kai tsaye, babu bukatar wani abu da za a gudanar a duk lokacin da aka nemi buƙatar. Sabili da haka yana da ƙananan kayan aiki kamar yadda ake amfani da plugin, misali.

* Lura: Apache yana da game da 30% kasuwar karɓar kudi bisa ga rahoton W3Techs. Mafi shahararrun shahararren sunayen da aka ambata a WHSR - A2 Hosting, BlueHost, GreenGeeks, Hostinger, InMotion Hosting, SiteGround; Ana amfani da Apache ne.

Abin da .htaccess ne Domin?

Kodayake akwai dot a gaban sunan filename, htaccess shine sunan fayil kuma dot bai sanya shi tsawo ba. Musamman, wannan ainihin filename ne wanda Apache ya dubi lokacin da yake gudana. Duk wani abu a cikin fayil na .htaccess ya kafa sigogi don Apache don bawa ko musaki ayyuka ko ma kashe ayyuka na musamman idan an cika wasu yanayi.

Alal misali, bisa umarnin da ke ƙunshe a cikin fayil ɗin, Apache na iya ɗaukar shafukan kuskuren al'ada ta atomatik idan shafukan yanar gizonku suna neman hanyar da ba a can ba. Kowane nau'in kuskure yana da lambar kansa kuma kowane ɗayan waɗannan za'a iya saukewa daban-daban.

Akwai babban jerin abubuwan da za a iya yi ta amfani da fayil .htaccess kuma a yau zan raba wasu daga cikin waɗannan tare da ku.

1. Sarrafa Shafukan Kuskuren Custom

Caption: Misalan wasu shafuka masu kuskure na 404 da aka kirkiro

Lokacin da aka bar a cikin saitunan tsoho, yawancin software na yanar gizon yana aikawa da kuskuren kuskure ga masu baƙi. Idan kana so ka nuna karin kuskuren mai amfani (ko a kalla, kyauta) kuskuren shafin sa'an nan kuma za a buƙaci amfani da matsala daftarin rubuce-rubuce na al'ada a cikin fayil din .htaccess.

Bari mu ce ka tsara wani Shafin kuskure na 404 na al'ada da ake kira "404.html" kuma ya ajiye shi a cikin babban fayil a cikin shafukan yanar gizo da ake kira "error_pages". Yin amfani da layi na code a .htaccess, za ka iya kiran wannan shafin a duk lokacin da wani baƙo ya sadu da kuskuren 404;

KuskureDocument 404 /error_pages/404.html

Amfani da wannan ƙira, za ka iya ajiye kwafin kwarai na kowane ɓataccen kuskure ɗin da kake so ka lissafa kuma saita su zuwa kiran da ya hada da lambar da ake bukata a cikin fayil din .htaccess. Mafi yawan ci karo kuskuren kuskure sun hada da;

  • 400 - Bincike mara kyau
  • 403 - An haramta
  • 404 - Ba a samo fayil ba
  • 500 - Kuskuren Cibiyar Intanet
  • 503 - Babu sabis

2. Yi Jagorar Gyara

Akwai wasu lokatai lokacin da kake so ka sanya bargo a madaidaici domin ka iya jagorantar baƙi zuwa takamaiman shafuka ba tare da sanin su ba. Alal misali, idan kuna amfani da HTTP a asali amma tun daga wancan lokacin shigar SSL kuma koma zuwa HTTPS, kuna so dukan masu amfani da ku don amfani da HTTPS version of your site.

A wannan yanayin, abin da kake buƙatar yin shine yin amfani da mulkin Rewrite;

Sake rubutawa A kan Rubutawa% {SERVER_PORT} 80 RewriteRule ^ (. *) $ Https://www.yourdomain.com/$1 [R, L]

Lambar don wannan za'a iya canzawa dangane da bukatunku. Alal misali, idan kana so ka tura masu amfani daga tsoffin yankin zuwa wani sabon wuri, to, za ka yi amfani da su;

A sake rubutawa a kan RewriteCond% {HTTP_HOST} ^ olddomain.com [NC, OR] sake rubutawa% {HTTP_HOST} ^ www.olddomain.com [NC] sake rubutawa ^ (. *) $ Http://newdomain.com/$1 [L, R = 301, NC]

Akwai bambanci da dama yadda za a iya saita canje-canje. Ƙarin bayanai na sabuntawa don Rewrite za a iya samu a cikin Takardun Apache shafuka.

Ɗaya daga cikin abubuwan da suka fi dacewa da za su iya yi maka shine don taimaka maka jagorancin injunan bincike zuwa shafukan da ka motsa. Yawancin lokaci, injunan bincike za su haɗin haɗin linzamin kwamfuta kuma idan ba za su iya samun shafuka masu dacewa a can za su ɗauka cewa abin ya ɓace ba.

Ta amfani da hanyar turawa, zaka iya sauke abun ciki kuma bari mahaukaciyar yanar gizo su san inda za su sami abun da suka kasance a baya. Don yin haka, amfani;

A tura 301 / archive / / past-entries /

Ka'idojin 301 ba kawai bari masu amfani su shiga tsohuwar abun ciki ba, amma har ila yau suna yin amfani da su ga masu amfani da fasahar yanar gizon cewa an motsa abun ciki har abada. Wannan yana taimaka musu ta hanyar barin su su sake maimaita jigilar hanyoyi.

2. Ƙara Tsaron Yanar Gizo naka

Na ga cewa yawancin sababbin masu amfani da yanar gizon sun kasance suna dogara sosai a kan kayan aiki na waje don ƙara yawan tsaro na yanar gizo. Yayinda yake da gaskiya cewa akwai aikace-aikacen aikace-aikace masu yawa a can, za ka iya fara tare da mahimman bayanai a cikin fayil din .htaccess.

Adireshin Tsare Sirri

Don yin wannan za ku buƙaci fayiloli guda biyu, .htaccess da .htpasswd. Fayil .htpasswd ya hada da wasu boye-boye, don haka amfani da kayan aiki kamar Mai gabatarwa don ƙirƙirar fayil. Fayil .htaccess ya hada da wannan lambar;

AuthType Basic AuthName "Yankin Kare Kalmomin Kalmar" AuthUserFile /path/to/.htpasswd Bukatar mai amfani mai amfani

Gano Tarihin Bincike

Wannan yana daga cikin mafi sauki don yin kuma yana buƙatar lambobi biyu kawai don a hada su a cikin fayil .htaccess ɗinku;

# Dakatar da Zaɓuɓɓukan Bincike na Tarihi -Indexes

Tare da takamaiman IPs

Don toshe mutum IPs daga ziyartar shafinku, ƙara da wadannan lambobi zuwa fayil dinku .htaccess;

Karyata daga XXX.XXX.XXX.XXX

Inda za ku maye gurbin XXX tare da lambobin IP na lambobi. Akwai bambancin wannan lambar da za a iya amfani dashi don toshe wani kewayon adiresoshin IP ko adiresoshin IP masu yawa.

3. Yarjejeniyar Hotlink

Hotlinking yana faruwa yayin da wasu shafukan yanar gizo suka haɗa zuwa hotuna da kake hosting. Wannan ba mai kyau ba ne saboda suna amfani da duka sararin samaniya tare da bandwidth. Don hana image hotlinking, ƙara da wadannan zuwa your .htaccess fayil;

A sake rubutawa a kan RewriteCond% {HTTP_REFERER}! ^ $ RewriteCond% {HTTP_REFERER}! ^ Http: // (www \.) Misali /.*$ [NC] RewriteRule \. (Gif | jpg | jpeg | bmp) $ - [F]

Sakamakon karshe na wannan lambar ita ce inda ka ƙayyade fayilolin da kake son toshe shafuka daga hotlinking. Yana koyar da Apache don toshe duk hanyoyi zuwa fayiloli waɗanda basu daga yankin suna http://www.example.com/.

Mutane da yawa waɗanda hotlink basu taba duba su ba, don haka idan kuna so su kunyata su, za ku iya nuna sakon al'ada zuwa shafukan da ke kokarin hotlink;

A sake rubutawa a kan RewriteCond% {HTTP_REFERER}! ^ $ RewriteCond% {HTTP_REFERER}! ^ Http: // (www \.) Misali /.*$ [NC] Sake rubutawa \. (Gif | jpg) $ http: // www .example.com / stopstealing.gif [R, L]

Wanne zai iya nuna wani hoto dabam a kan shafin da ke damun kamar haka:

4. Kare fayil .htaccess

By yanzu kuna ganin yadda kayan aiki ke amfani da shi .htaccess fayil ne. Tun da ka zo ga wannan fahimtar, lokaci ne da ka yi tunanin kare wannan mahimmin fayil! Idan kana amfani da fayiloli na .htpasswd, to tabbas za ka so ka kare wannan da kuma yadda za a yi dukansu shi ne;

# kare .htaccess da .htpasswd <Files ~ "^. * \. [[Hh] [Tt])"> Saya ya bada izinin, ƙaryatãwa game da Saki daga duk Satisfy duk </ Files>

Yi la'akari da cewa a kan sabobin tsaro mafi yawa waɗannan fayiloli an riga an kare su. Kafin ka ƙara wannan lambar duba don ganin idan zaka iya samun dama ga fayiloli a cikin taga mai bincike. Rubuta kawai a cikin URL kuma ƙara a /.htaccess a baya don ganin idan zaka iya duba fayil din. Idan ba za a iya nuna maka kuskure ba.

5. Ƙaddamar da Yanayin Lokacin Sadarwarku

Idan ka lura cewa saboda wani dalili ko wasu, lokuta a kan uwar garke suna neman su kashe dan kadan sa'annan zaka iya buƙatar tilasta yankin lokaci ta amfani da fayil .htaccess. Wannan abu ne mai sauƙi kuma ya buƙaci kawai guda guda na lambar;

SetEnv TZ America / lokacinku

Akwai jerin jerin lokuttan lokaci waɗanda ke samuwa kuma zaka iya samun mafi kyau wasa da naka ta hanyar magana akan jerin lokuttan lokaci masu goyan baya.

Biye da Power of .htaccess

Wadannan samfurori da na nuna a nan sune ƙarshen babban kankara. Yana ba masu amfani da yanar gizon masu amfani da yawa mafi iko a kan shafukan su fiye da yadda za a yarda su ta hanyar hanyar kula da su sannan su ba su kayan aiki masu yawa don aiki tare.

Koyo don yin amfani da lambar ita ce hanya kawai da ikon Google, kuma mafi kyawun abu shi ne cewa kawai kuna buƙatar ɗauka da koya abubuwan da kuke bukata don amfani.

Game da Jerry Low

Founder of WebHostingSecretRevealed.net (WHSR) - nazarin bita da aka yi amfani da shi ta hanyar masu amfani da 100,000. Fiye da shekaru 15 kwarewa a yanar gizon yanar gizon, tallace-tallace haɗin gwiwa, da SEO. Mai ba da gudummawa ga ProBlogger.net, Business.com, SocialMediaToday.com, da sauransu.

n »¯