- Bùi Tấn Lực
- 535
- 08/10/2024
File .htaccess là gì? Đó là một file cấu hình cấp cao của trang web chạy trên máy chủ web Apache, nó thay đổi được các giá trị mặc định của apache, chi tiết như nào hãy cùng chúng tôi phân tích:
File .htaccess là gì? File .htaccess dùng để làm gì
Mục Lục [Ẩn]
File .htaccess là gì?
File .htaccess là một file cấu hình cấp cao của trang web chạy trên máy chủ web Apache, nó thay đổi được các giá trị được set mặc định của apache bằng cách chỉnh trực tiếp trên file mà không cần phải chỉnh sửa file cấu hình server, ví trí nằm ở ngay thư mục gốc của bộ source trang web.
File .htaccess dùng để làm gì?
Bài viết này mình đang để mặc định tên miền webmoi.vn bên mình, hãy thay tên miền này thành tên miền bên bạn:
Thêm www vào domain
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^webmoi.vn [NC]
RewriteRule ^(.*)$ https://www.webmoi.vn/$1 [L,R=301]
Bỏ www ra khỏi domain
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^www.webmoi.vn [NC]
RewriteRule ^(.*)$ https://webmoi.vn/$1 [L,R=301]
Cấu hình https cho website
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Redirect đến trang thông báo lỗi riêng với file .htaccess
Các lỗi phổ biến trên website như: 401, 403, 404, 500 … 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 nếu không redirect, để chuyển hướng đến trang thông báo lỗi bạn có thể dùng file .htaccess:
ErrorDocument 400 https://webmoi.vn/
ErrorDocument 401 https://webmoi.vn/
ErrorDocument 403 https://webmoi.vn/
ErrorDocument 404 https://webmoi.vn/404.html
ErrorDocument 500 https://webmoi.vn/
Xem thêm bài viết:
Lỗi 500 Internal Server Error là gì
Cài đặt 301 Redirect với file .htaccess
Redirect 301 để chuyển hướng link cũ đã có trafic về link mới.
RewriteRule ^xay-dung-landingpage-de-thu-hut-khach/$ https://webmoi.vn/landing-page-la-gi/ [R=301,L]
Chuyển .php sang .html (link không .php) trong url
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?com=$1 [R=301,L]
Ví dụ link đã chuyển hướng về là https://webmoi.vn/file-htaccess-la-gi/, nếu cần đổi thảnh .html thì bạn đổi ([a-zA-Z0-9_-]+)/ thành ([a-zA-Z0-9_-]+).html
Cấm IP truy cập vào site
allow from all
deny from 193.128.14.12
Giới hạn dung lượng file Upload
LimitRequestBody 10240000
# Giới hạn đến 10mb
Dùng .htaccess redirect tên miền cũ qua tên miền mới
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://www.newdomain.com/$1 [R=301,L]
Tham khảo file hoàn chỉnh của 1 trang web
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^www.webmoi.vn [NC]
RewriteRule ^(.*)$ https://webmoi.vn/$1 [L,R=301]
ErrorDocument 400 https://webmoi.vn/
ErrorDocument 401 https://webmoi.vn/
ErrorDocument 403 https://webmoi.vn/
ErrorDocument 404 https://webmoi.vn/404.html
ErrorDocument 500 https://webmoi.vn/
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^thiet-ke-web-tai-quan-8/$ https://webmoi.vn/thiet-ke-website-tai-quan-8/ [R=301,L]
RewriteRule index.html$ index.php
RewriteRule rss.xml$ rss.php
#ĐĂNG NHẬP - ĐĂNG KÝ
RewriteRule ^dang-nhap.html$ index.php?com=dang-nhap&act=login [L]
RewriteRule ^404.html$ index.php?com=404 [L]
RewriteRule ^thoat.html$ index.php?com=dang-nhap&act=logout [L]
RewriteRule ^quen-mat-khau.html$ index.php?com=dang-nhap&act=quen-mat-khau [L]
RewriteRule ^thong-tin-ca-nhan.html$ index.php?com=dang-nhap&act=user-info [L]
RewriteRule ^doi-mat-khau.html$ index.php?com=dang-nhap&act=doi-mat-khau [L]
#SƠ ĐỒ TRANG
#RewriteRule site-map.xml$ site.php
RewriteRule sitemap.xml$ site.php
#ADMIN ---------------------
RewriteRule ^quantri$ quantri/index.php?com=user&act=login [L]
RewriteRule ^quantri/$ quantri/index.php?com=user&act=login [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(.*) /$1?%2 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)zarsrc=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(.*) /$1?%2 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)gidzl=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(.*) /$1?%2 [R=301,L]
</IfModule>
RewriteRule ^([a-zA-Z0-9_-]+)//$ https://webmoi.vn/$1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ https://webmoi.vn/$1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ https://webmoi.vn/$2/ [R=301,L]
#RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?com=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?com=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/p=([0-9]+)$ index.php?com=$1&p=$2 [L]
RewriteRule ^thumb/([0-9]+)x([0-9]+)/([0-9]+)/(.*)$ timthumb.php?w=$1&h=$2&zc=$3&q=99&src=https://%{HTTP_HOST}/$4 [L]
RewriteRule ^upload/images/(.*)$ timthumb.php?w=0&h=0&zc=1&q=99&src=https://%{HTTP_HOST}/upload/images/$1 [L]
#<Ifmodule !mod_php5.c>
#AddHandler x-httpd-php52 .php
#AddHandler x-httpd-php .php
### end content - please not remove this line
###End Kloxo PHP config Area
# Enable Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-woff2
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
</IfModule>
<IfModule mod_gzip.c>
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.*
</IfModule>
# Leverage Browser Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/svg "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
<IfModule mod_headers.c>
<filesmatch ".(ico|flv|jpg|jpeg|png|svg|gif|css|swf|webp|ttf|woff2)$">
Header set Cache-Control "max-age=31536000, public"
</filesmatch>
<filesmatch ".(html|htm)$">
Header set Cache-Control "max-age=31536000, private, must-revalidate"
</filesmatch>
<filesmatch ".(pdf)$">
Header set Cache-Control "max-age=31536000, public"
</filesmatch>
<filesmatch ".(js)$">
Header set Cache-Control "max-age=31536000, private"
</filesmatch>
</IfModule>
Lưu ý khi sử dụng .htaccess
Hãy tải file .htaccess về máy tính và lưu lại để phòng khi trường hợp cần thì lấy ra cập nhật lại, không phải tự nhiên mà nhắc tới vấn đề này vì file này một khi lỗi một dấu chấm nhỏ cũng đủ làm khó hoặc bị sai một cấu hình nào đó cũng khó sửa được. Có trong dân chuyên ngành cũng nên lưu lại vì một khi bị sự cố kỹ thuật làm xóa trắng file thì lúc đó xây lại rất khó, thậm chí nhiều ông còn không biết xây lại như nào vì đã tạo nhiều thứ trong đó.
Còn người sử dụng website thì tốt nhất đừng có mò mẫm gì trong này, vô đó phá chứ không chỉnh được thứ gì trong đó đâu.
Lời kết
Cảm ơn các bạn đã tham khảo bài viết File .htaccess là gì?
- 0 Bình luận
Email, Điện thoại của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *