Cấu hình htaccess cơ bản

1. Bỏ hoặc theme www vào domain:

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

// Không theme WWW vào URL

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^domain.com$ [NC]

RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

2. Redirect đến trang thông báo lỗi riêng

Trong web thì có nhiều lỗi như: 401, 403, 404, 500 … là các lỗi phổ biến, nếu không redirect nó sẽ trả về trình duyệt của người dùng một thông báo rất xấu xí, không chuyên nghiệp và tạo cảm giác khó chịu khi bị lỗi, bạn có thể dùng file .htaccess để chỉnh redirect đến trang thông báo lỗi mà bạn đã định sẳn.

ErrorDocument 401 /error/401.php

ErrorDocument 403 /error/403.php

ErrorDocument 404 /error/404.php

ErrorDocument 500 /error/500.php

3. 301 Redirect có lợi cho SEO:

301 Redirect là vấn đề mà mình đã đề cập ở bài trước tối ưu seo với 301 Redirect, đây là phương pháp được xem là tối ưu nhất cho việc chuyển tên miền hay chuyển file nào đó mà không mất về lượng truy cập cũng như về kết quả tìm kiếm trên google

// 1 Trang

Redirect 301 /old/old.htm http://domain.com/new.htm

// chuyển domain

RewriteEngine On

RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L

Hotlink chính là link trực tiếp đến trang web của người khác lấy các file về sử dụng trên website, có thể là file hình ảnh, video, tài liệu … Hotlink là một giải pháp cho những blog tạm, không chắc chắn và những ai hay đi copy bài người viết của các trang web khác. Hotlink sẽ làm tốn một lượng băng thông rất lớn vì các site khác lấy file trực tiếp từ host của bạn

Options +FollowSymlinks

#No hotlink

RewriteEngine On

RewriteCond %{HTTP_REFERER}!^$

RewriteCond %{HTTP_REFERER}!^http://(www.)?domain.com/[nc]

RewriteRule .*.(gif|jpg|png)$ //domain.com/images/nohotlink.gif[nc]

5. Bỏ đuôi mở rộng của file web “.php”

RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

6. Chuyển .php sang .html

RewriteRule ^(.*).html$ $1.php [R=301,L]

7. Thêm dấu “/” vào cuối URL

#Thêm / vào cuối URL

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !#

RewriteCond %{REQUEST_URI} !(.*)/$

RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

8. Cấm IP truy cập

allow from all

deny from 192.168.1.123

deny from 192.168

9. Bảo vệ file trong host

<files .htaccess="">

order allow,deny

deny from all

</files>

10. Đặt password cho thư mục và file

resides

AuthType basic

AuthName "Thư mục này đã được bảo vệ"

AuthUserFile /home/path/.htpasswd

AuthGroupFile /dev/null

Require valid-user

# Đặt Pass cho file

<files secure.php="">

AuthType Basic

AuthName "Prompt"

AuthUserFile /home/path/.htpasswd

Require valid-user

</files>

11. Chuyển dấu “_” thành “-” trong URL

Options +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteRule !.(html|php)$ - [S=4]

RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]

RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]

RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]

RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]

RewriteCond %{ENV:uscor} ^Yes$

RewriteRule (.*) http://domain.com/$1 [R=301,L]

12. Set lại trang mặc định

DirectoryIndex info.html

13. Bật tính năng nén file Gzip

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript

BrowserMatch ^Mozilla/4 gzip-only-text/html

BrowserMatch ^Mozilla/4.0[678] no-gzip

BrowserMatch bMSIE !no-gzip !gzip-only-text/html

5. Không cho truy cập file wp-config.php

<files wp-config.php>

order allow,deny

deny from all

</files>

# Giới hạn đến 10mb

LimitRequestBody 10240000

K