.post IMG { max-width:550px; width: expression(this.width > 550 ? 550: true); max-height:520px; height: expression(this.height > 520 ? 520: true); }

Các thủ thuật cần thiết nhất về file .htaccess

Written By Unknown on Thứ Bảy, 26 tháng 7, 2014 | 00:02

Bài viết này hướng dẫn bạn cách tạo và một số thủ thuật nhỏ về file .htaccess

Về file này, thì bạn có thể lên google search được rất nhiều bài viết của các tác giả về file .htaccess rồi.

Làm thế nào để tạo file .htaccess?
Mở một bộ soạn thảo của bạn, sau đó save lại với tên .htaccess và kích hoạt mod_rewrite trong file php.ini của Apcache Web Server
Default directory Listing


Cấm truy cập một thư mục nào đó

Để cấm người dùng truy cập vào một thư mục nào đó, chúng ta dùng đoạn code sau:
1 # Disable Directory Browsing
2 Options All -Indexes
- See more at: http://sharecodeweb.net/cac-thu-thuat-ve-file-htaccess/#sthash.Dn2DZCby.dpuf
Khi đó, người dùng cố truy cập vào thì sẽ gặp lỗi như sau:


Trang Error Ở đây trang chứa thông báo lỗi của mình là error.html, trang này sẽ xuất hiện khi gặp một số lỗi nhất định như 400, 401,404, 500.
1 errorDocument 400 http://www.youwebsite.com/error.html
2 errorDocument 401 http://www.youwebsite.com/error.html
3 errorDocument 404 http://www.youwebsite.com/error.html
4 errorDocument 500 http://www.youwebsite.com/error.html

Ngoài việc cho hiển thị chung một file error.html thì bạn cũng có thể tùy chọn cho hiển thị bất cứ file nào tùy thích.


Các bạn cũng chú ý xem là Rewrite Rules đã kích hoạt chưa nhé.
1 RewriteEngine on

Domain Redirection
Nếu bạn muốn redirect domain từ dạng có yourname.com sang www.yourname.com thì làm như sau - See more at:
1 RewriteCond %{HTTP_HOST} ^yourwebsite.com
2 RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub domain redirect

Nếu muốn redirect subdomain thì bạn làm như sau:
1 RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
2 RewriteCond %{REQUEST_URI} !^/subdomain_folder/
3 RewriteRule (.*) /subdomain_folder/$1

Ngoài ra, nếu muốn http://yourname.com kết nối với website_folder thì làm như sau:

1 RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
2 RewriteCond %{REQUEST_URI} !^/website_folder/
3 RewriteRule (.*) /website_folder/$1

Redirect domain cũ về domain mới

1 RewriteCond %{HTTP_HOST} ^abc.com
2 RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]
3 RewriteCond %{HTTP_HOST} ^www\.abc\.com
4 RewriteRule (.*) http://www.abc.com/$1 [R=301,L]

Profile Url

Việc này sẽ redirect từ http://labs.9lesson.info/profile.php?username=srinivas thành http://labs.9lesson.info/srinivas. Việc này có sử dụng Regular Expression giúp cho Url của chúng ta thân thiện hơn.
1 RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
2 RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

Message Url

Redirect từ http://labs.9lesson.info/messages.php?message_username=srinivas thành http://labs.9lesson.info/messages/srinivas
1 RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1
2 RewriteRule ^messages/([a-zA-Z0-9_-]+)/$ messages.php?message_username=$1

Friendly url

Redirect từ http://labs.9lesson.info/friends.php?username=srinivas thành http://labs.9lesson.info/friends/srinivas
1 RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
2 RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1

Friendly url với 2 đối số

Redirect từ http://labs.9lesson.info/friends.php?username=srinivas&page=2 thành http://labs.9lesson.info/friends/srinivas/2
1 RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
2 RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

Ẩn phần mở rộng của file

Redirect từ http://www.yourwebsite.com/index.html thành http://www.yourwebsite.com/index
1 RewriteRule ^([^/.]+)/?$ $1.html

Download code Vậy là xong rồi đó, chúc bạn thành công
Share this article :

0 nhận xét:

Đăng nhận xét

 
Tài trợ : Đà Nẵng, Tải portable games miễn phi
Copyright © 2013. Tải portable softwares miễn phí - All Rights Reserved
Address: 18 tầng địa ngục - 9 tầng mây - 0 tầng hầm
Liên hệ: Phone: 0909090909 / Mail: topdanangcom@gmail.com