• August 10, 2017

    .htaccess คือ วิธีการอีกหนึ่งวิธีที่ใช้สำหรับ config เปลี่ยนค่าต่างๆ ของแต่ละ directory หลักๆ ภายในไฟล์อาจจะประกอบไปด้วย 1 หรือหลายๆคำสั่ง โดยไฟล์ .htaccess จะถูกเจาะจงวางใว้แค่บาง directory และจะบังคับใช้คำสั่งภายใน directory และ sub directory นั้นๆ

    ซึ่งผมได้รวบรวมเทคนิคที่เป็นประโยชน์และสำคัญๆใว้ดังนี้ครับ

    1. Custom Directory Index Files

    DirectoryIndex index.html index.php index.htm

    คุณสามารถเปลี่ยนค่า default index file ได้โดยใช้คำสั่งข้างบน โดยระบุไฟล์ที่คุณต้องการ แทน index.html index.phpindex.htm ตามลำดับ

    2. Custom Error Pages

    ErrorDocument 404 errors/404.html

    ใช้สำหรับ redirect user ไปหน้าที่คุณต้องการ ถ้าเกิดเหตุการที่ user เรียกหน้าที่ไม่มีในระบบ (เกิด error 404)

    3. Control access at files & directory level

    จำกัดหรือปฏิเสธการเข้าใช้ไฟล์หรือโฟลเดอร์ที่เป็นส่วนบุคคล เช่น โฟลเดอร์ includes ที่เก็บไฟล์สคริปต่างๆ
    ในกรณีนี้ให้คุณสร้างไฟล์ .htaccess ในโฟลเดอร์ includes แล้วใส่คำสั่งดังนี้

    # no one gets in here!
    deny from all

    จะปฏิเสธทุก direct access และทุกไฟล์ที่อยู่ในโฟลเดอร์นั้น
    คุณสามารถระบุเงื่อนไขได้เช่นกัน โดยตัวอย่างนี้เป็นการจำกัดการเข้าถึงจากช่วงของ IP

    # no nasty crackers in here!
    order deny,allow
    deny from all
    allow from 192.168.0.0/24
    # this would do the same thing..
    #allow from 192.168.0

    หรือบางครั้งคุณอาจจะแค่ต้องการ ban เพียงแค่ IP เดียว

    # someone else giving the ruskies a bad name..
    order allow,deny
    deny from 
    83.222.23.219
    allow from all

    4. Modifying the Environment Variable

    Set / Unset environment variables โดยใช้ SetEnv and UnSetEnv.

    SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com
    
    UnSetEnv REMOTE_ADDR

    5. 301 Redirect using htaccess

    ถ้าคุณต้องการที่จะ redirect จากไฟล์เก่าไปไฟล์ใหม่

    Redirect 301 /old/file.html http://yourdomain.com/new/file.html

    ถ้าเป็นไดเรคทอรี่

    RedirectMatch 301 /blog(.*) http://yourdomain.com/$1

    6. Implementing a Caching Scheme with .htaccess

    Cache ข้อมูลที่มีการเปลี่ยนแปลงไม่บ่อย (static) เพื่อเพื่ม performance ระบบ

    # year
    
    Header set Cache-Control “public”
    Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
    Header unset Last-Modified
    
    #2 hours
    
    Header set Cache-Control “max-age=7200, must-revalidate”
    
    SetOutputFilter DEFLATE
    Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”

    7. Compress output using GZIP

    บีบอัดไฟล์ css, js, html, ด้วย Gzip

    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

    โดยโค้ดข้างบนจะทำงานถ้าเว็บเซอร์เวอร์ของคุณเปิด mod_gzip คุณอาจจะต้องเพิ่มโค้ดข้างล่างนี้
    ถ้าเว็บเซอร์เวอร์ของคุณสนับสนุน mod_deflate

    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \
    \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \
    \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary

    ถ้าเว็บเซอร์เวอร์ของคุณไม่สนันสนุน mod_deflate คุณอาจต้องการเพิ่มโค้ดนี้

    php_value output_handler ob_gzhandler

    8. Redirect browser to https (ssl)

    redirect เว็บไซด์ไปใช้ https

    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    9. Rewrite URLs using htacccess

    เปลี่ยน URL จาก product.php?id=12 เป็น product-12.html

    RewriteEngine on
    RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

    เปลี่ยน URL จาก product.php?id=12 เป็น product/ipod-nano/12.html

    RewriteEngine on
    RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

    เปลี่ยน URL ที่ไม่มี www ให้มี www (ป้องกัน Duplicate Content)

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^viralpatel\.net$
    RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]

    เปลี่ยน URL จาก yoursite.com/user.php?username=xyz เป็น yoursite.com/xyz

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

    Redirect domain ไปหา subfolder ภายใน public_html

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    RewriteCond %{REQUEST_URI} !^/new/
    RewriteRule (.*) /new/$1

    10. Prevent Directory Listing

    หลีกเลี่ยง directory listing

    Options -Indexes

    หรือ

    IndexIgnore *

    11. Adding new MIME types

    ประเภทขึ้นอยู่กับนามสกุลไฟล์ นามสกุลที่ไม่รู้จักจะถูกเปลี่ยนเป็นข้อความ และจะเกิดความเสียหาย

    AddType application/x-endnote-connection enz
    AddType application/x-endnote-filter enf
    AddType application/x-spss-savefile sav

    12. Deny access to static file data

    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC]
    RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ – [F,NS,L]

    13. Specify Upload file limit for PHP in htaccess

    php_value upload_max_filesize 20M
    php_value post_max_size 20M
    php_value max_execution_time 200
    php_value max_input_time 200

    บรรทัดแรกคือการจำจัดค่าสูงสุดของไฟล์ที่สามารถอัพโหลดได้
    บรรทัดสองคือการจำกัดค่าสูงสุดของข้อมูลการโพส
    บรรทัดสามคือค่าเวลาในการ execution
    บรรทัดสุดท้ายคือค่าเวลาสูงสุดที่อนุญาติในการ input ข้อมูลอัพโหลดเช่น POST and GET

    14. Disallow Script Execution

    Options -ExecCGI
    AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

    15. Change Charset and Language headers

    AddDefaultCharset UTF-8
    DefaultLanguage en-US

    16. Set Timezone of the Server (GMT)

    SetEnv TZ America/Indianapolis

    รายชื่อของเขตเวลาที่สนับสนุน

    http://www.php.net/manual/en/timezones.php

    17. Force “File Save As” Prompt

    AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

    18. Protecting a single file

    โดยปกติ .htaccess จะมีผลกับข้อมูลทั้งไดเรคทอรี่ แต่คุณสามารถที่จะแก้ไขคำสั่งแบบเจาะจงกับบางไฟล์

    order deny,allow
    deny from all
    AuthType Basic
    AuthName “Characterology Student Authcate”
    AuthLDAP on
    AuthLDAPServer ldap://directory.characterology.com/
    AuthLDAPBase “ou=Student, o=Characterology University, c=au”
    require valid-user
    satisfy any

    19. Set Cookie using htaccess

    environment variable

    Header set Set-Cookie “language=%{lang}e; path=/;” env=lang

    สร้าง cookie ในเครื่อง client กับข้อมูลที่ matching กัน

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ – [co=lang:$2:.yourserver.com:7200:/]

    20. Send Custom Headers

    Header set P3P “policyref=\”http://www.askapache.com/w3c/p3p.xml\””
    Header set X-Pingback “http://www.askapache.com/xmlrpc.php”
    Header set Content-Language “en-US”
    Header set Vary “Accept-Encoding”

    21. Blocking request based on User-Agent Header

    SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
    SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
    Deny from env=HTTP_SAFE_BADBOT

    22.Prevent hacks

    ถ้าคุณต้องการที่จะเพิ่มระดับความปลอดภัยให้กับระบบคุณ คุณสามารถเพิ่มโค้ดเพียงไม่กี่บรรทัดนี้ เพื่อตรวจสอบ url ที่แปลกๆที่มีพิรุทเสี่ยงจะเป็นอัตราย

    RewriteEngine On
    
    # proc/self/environ? noway!
    RewriteCond %{QUERY_STRING} proc/self/environ [OR]
    
    # Block out any script trying to set a mosConfig value through the URL
    RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
    
    # Block out any script trying to base64_encode crap to send via URL
    RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]

     show case

    ครอบจักรวาล

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+) - [PT,L]
    
    RewriteRule ^(.*) $1/ [R]
    RewriteRule ^(.*) index.php
    


เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories