PHP: Một số cách sử dụng .htaccess để cấu hình website - P3

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
  • Kiểm tra URL
    1
    2
    3
    4
    # automatically corect simple speling erors
    <IfModule mod_speling.c>
    CheckSpelling On
    </IfModule>
  • Sắp xếp lại trang thông báo lỗi: Cấu hình này rất hữu ích vì nó đưa ra lỗi cho người truy cập website một cách thân thiện, giúp cho các bạn có thể hiển thị các thông báo lỗi theo cách riêng.
    1
    2
    3
    4
    5
    6
    # server custom error pages
    ErrorDocument 400 /errors/400.html
    ErrorDocument 401 /errors/401.html
    ErrorDocument 403 /errors/403.html
    ErrorDocument 404 /errors/404.html
    ErrorDocument 500 /errors/500.html
  • Chỉ thị server hiển thị mã nguồn với một số file thực thi : Một số trường hợp cần hiển thị mã nguồn của một file thay vì thực thi chúng.
    1
    RemoveHandler cgi-script .pl .py .cgi
  • Redirect người dùng tới một site tạm thời khi phát triển hoặc sửa lỗi: Trong quá trình phát triển, bảo trì hay sửa chửa website, bạn không muốn khách hàng viếng thăm, cấu hình dưới sẽ giúp chuyển hướng người dùng tới một site khác trong khi quản trị viên vẫn có khả năng truy nhập đầy đủ (x.x.x.x là IP của quản trị):
    1
    2
    3
    4
    5
    # redirect all visitors to alternate site but retain full access for you
    Order deny,allow
    Deny from all
    Allow from x.x.x.x
  • Chặn truy cập tới file hay thư mục theo thời gian
    1
    2
    3
    4
    5
    6
    7
    8
    # prevent access during the midnight hour
    RewriteCond %{TIME_HOUR} ^12$
    RewriteRule ^.*$ - [F,L]
    # prevent access throughout the afternoon
    RewriteCond %{TIME_HOUR} ^(12|13|14|15)$
    RewriteRule ^.*$ - [F,L]
    Mask
    03/07/13, 05:20 PM
  • Kích hoạt SSI: Khi sử dụng chế độ SSI, phần mở rộng các file trên server phải ở dạng .shtml thay vì .html. Điều này gây bất lợi cho các website đã thiết lập dạng .html, để tránh phải chuyển đổi lại định dạng mở rộng file trên server.
    1
    AddHandler server-parsed .html

    Có thể thêm nhiều dòng để server chấp nhận nhiều định dạng khác nhau.Ví dụ:
    1
    2
    3
    AddHandler server-parsed .html
    AddHandler server-parsed .shtml
    AddHandler server-parsed .htm
  • Chặn truy cập tới các file include trong file .php: Để tránh bị truy cập tới thư mục chứa các file .php, tạo file .htaccess với nội dung sau và includes là thư mục chứa file .php.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ## Enable Mod Rewrite, this is only required once in each .htaccess file
    RewriteEngine On
    RewriteBase /
    ## Test for access to includes directory
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /includes/ .*$ [NC]
    ## Test that file requested has php extension
    RewriteCond %{REQUEST_FILENAME} ^.+\.php$
    ## Forbid Access
    RewriteRule .* - [F,NS,L]
  • Thủ thuật redirect: Đối với tất cả các loại redirect sử dụng mode_rewrite cần enable chế độ: RewriteEngine.
    1
    2
    # initialize and enable rewrite engine
    RewriteEngine on

    Redirect từ http://www.domain.com sang http://domain.com:
    1
    2
    3
    4
    5
    # permanently redirect from www domain to non-www domain
    RewriteEngine on
    Options +FollowSymLinks
    RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
    RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

    Redirect từ một domain cũ sang domain mới:
    1
    2
    3
    # redirect from old domain to new domain
    RewriteEngine On
    RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]

    Redirect String Variations sang một địa chỉ. Giả sử một request có chứa string: some-string, ta sẽ chuyển request này tới trang: http://some-string.com
    1
    2
    # redirect any variations of a specific character string to a specific address
    RewriteRule ^some-string http://www.some-string.com  [R]

    Một số phương pháp khác:
    1
    2
    3
    4
    # map URL variations to the same directory on the same server
    AliasMatch ^/director(y|ies) /www/docs/target
    # map URL variations to the same directory on a different server
    RedirectMatch ^/[dD]irector(y|ies) http://domain.com

    Redirect một site đầu vào với trạng thái 301:
    1
    2
    # redirect an entire site via 301
    redirect 301 / http://www.domain.com/

    Redirect một file với trạng thái 301:
    1
    2
    # redirect a specific file via 301
    redirect 301 /current/currentfile.html http://www.newdomain.com/new/newfile.html

    Redirect một site qua một redirect liên tục:
    1
    2
    # redirect an entire site via permanent redirect
    Redirect permanent / http://www.domain.com/

    Redirect một trang hoặc một thư mục với redirect liên tục:
    1
    2
    3
    # redirect a page or directory
    Redirect permanent old_file.html http://www.new-domain.com/new_file.html
    Redirect permanent /old_directory/ http://www.new-domain.com/new_directory/

    Redirect một file sử dụng RedirectMatch:
    1
    2
    # redirect a file using RedirectMatch
    RedirectMatch 301 ^.*$ http://www.domain.com/index.html

    Khi redirect các file, sử dụng rule Redirect với các file trong cùng domain, sử dụng rule RewriteRule cho bất cứ domain nào. rule RewriteRule mạnh hơn rule Redirect.
    Rule này cho phép tất cả các visitor xem page thông qua sub-domain.
    1
    2
    3
    4
    # send visitors to a subdomain
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$ [NC]
    RewriteRule ^/(.*)$ http://subdomain.domain.tld/$1 [L,R=301]

    Một số redirect khác:
    1
    2
    3
    # rewrite only if the file is not found
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)special\.html?$ cgi-bin/special/special-html/$1
     
    1
    2
    3
    # rewrite only if an image is not found
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule public/images/article/special/(.*).gif cgi-bin/special/mkgif?$1
     
    1
    2
    3
    4
    5
    # seo-friendly rewrite rules for various directories
    RewriteRule ^(.*)/aud/(.*)$ $1/audio-files/$2 [L,R=301]
    RewriteRule ^(.*)/img/(.*)$ $1/image-files/$2 [L,R=301]
    RewriteRule ^(.*)/fla/(.*)$ $1/flash-files/$2 [L,R=301]
    RewriteRule ^(.*)/vid/(.*)$ $1/video-files/$2 [L,R=301]
     
    1
    2
    3
    4
    5
    6
    # broswer sniffing via htaccess environmental variables
    RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
    RewriteRule ^/$ /index-for-mozilla.html [L]
    RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
    RewriteRule ^/$ /index-for-lynx.html [L]
    RewriteRule ^/$ /index-for-all-others.html [L]
     
    1
    2
    3
    4
    5
    # redirect query to Google search
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} .google\.php*
    RewriteRule ^(.*)$ ^http://www.google.com/search?q=$1 [R,NC,L]
     
    1
    2
    3
    # deny request according to the request method
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD)$ [NC]
    RewriteRule ^.*$ - [F]
     
    1
    2
    3
    # redirect uploads to a better place
    RewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC]
    RewriteRule ^(.*)$ /cgi-bin/upload-processor.cgi?p=$1 [L,QSA]
     
    1
    2
    # seo friendly redirect for a single file
    Redirect 301 /old-dir/old-file.html http://domain.com/new-dir/new-file.html
     
    1
    2
    # redirects all files in dir directory with first letters xyz
    RedirectMatch 301 /dir/xyz(.*) http://domain.com/$1
     
    1
    2
    # seo friendly redirect entire site to a different domain

Nguồn: http://wiki.matbao.net/Mot-so-cach-su-dung-htaccess-de-cau-hinh-website-P3.ashx 

» Tiếp: Chuẩn PSR trong PHP là gì
« Trước: Một số cách sử dụng .htaccess để cấu hình website - P2
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!