htaccess文件使用大全
- 威海網站建設 威海htaccess
- 1720
.htaccess文件是一種包含配置信息的文本文件,它可以用來配置Web服務器,從而實現許多各種各樣的功能。以下是.htaccess文件使用的一些常見場景:
1. 重定向URL
RewriteEngine On
RewriteRule ^example\.html$ /newexample.html [R=301,L]
上述代碼將example.html重定向到newexample.html,同時返回301狀態碼。
2. 防盜鏈
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
對于除了example.com之外的所有網站,對訪問.png,.jpg,.jpeg和.gif文件的請求將返回403 Forbidden錯誤。
3. 禁止IP地址
order deny,allow
deny from 192.168.1.1
allow from all
該代碼段會拒絕192.168.1.1的所有請求,允許其他請求。
4. 壓縮文件
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
此代碼段使用DEFLATE壓縮類型對文本、XML、JavaScript和PHP文件進行壓縮。
5. 配置文件緩存