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 là gì? ý nghĩa thế nào 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 2 | # Disable Directory Browsing Options All -Indexes |
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 2 3 4 | errorDocument 400 http://www.youwebsite.com/error.html errorDocument 401 http://www.youwebsite.com/error.html errorDocument 404 http://www.youwebsite.com/error.html 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
1 2 | RewriteCond %{HTTP_HOST} ^yourwebsite.com 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 2 3 | RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$ RewriteCond %{REQUEST_URI} !^/subdomain_folder/ 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 2 3 | RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$ RewriteCond %{REQUEST_URI} !^/website_folder/ RewriteRule (.*) /website_folder/$1 |
Redirect domain cũ về domain mới
1 2 3 4 5 | RewriteCond %{HTTP_HOST} ^abc.com RewriteRule (.*) http://www.xyz.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www\.abc\.com 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 2 | RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 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 2 | RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1 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 2 | RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1 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 2 | RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$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!
You must log in to post a comment.