最新消息:

htaccess 常用案例

htaccess admin 117浏览 0评论

不同域名 指向不同目录

RewriteEngine On
RewriteBase /

## ========    aaa123.aikaiyuan.com    ==========
RewriteCond %{HTTP_HOST} ^aaa123.aikaiyuan.com$ [NC]
RewriteCond %{REQUEST_URI} !^/aaa123/
RewriteRule ^(.*)$ aaa123/$1?Rewrite [L,QSA]

## ========    bbb321.aikaiyuan.cn    ==========
RewriteCond %{HTTP_HOST} ^bbb321.aikaiyuan.cn$ [NC]
RewriteCond %{REQUEST_URI} !^/bbb321/
RewriteRule ^(.*)$ bbb321/$1?Rewrite [L,QSA]

css,js文件 301 跳转

RewriteCond %{REQUEST_URI} ^.*(.css|.js)$
RewriteRule ^/data1/www/htdocs/258/aikaiyuan/1(.*) https://pic.aikaiyuan.com$1  [L,QSA,R=301] [L]

添加 Header 响应头

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|cur|bmp)$">
Header set Cache-Control "max-age=2592000"
Header set Aikaiyuan "HIT"
</FilesMatch>
# curl -s -I https://www.aikaiyuan.com/wp-content/uploads/2019/11/htaccess.jpg
HTTP/2 200
server: nginx
date: Fri, 22 Nov 2019 09:40:32 GMT
content-type: image/jpeg
content-length: 44491
last-modified: Fri, 22 Nov 2019 09:40:03 GMT
accept-ranges: bytes
cache-control: max-age=2592000
via: 10035
aikaiyuan: HIT

添加 expire 响应头

<IfModule mod_expires.c> 
ExpiresActive On 

ExpiresByType text/css A2592000
ExpiresByType application/javascript A2592000

ExpiresByType image/* A2592000

ExpiresByType application/x-gzip A86400
ExpiresByType application/zip A86400
ExpiresByType application/x-rar-compressed A86400

ExpiresDefault A600
</IfModule>
# curl -s -I https://www.aikaiyuan.com/wp-content/uploads/2019/11/htaccess.jpg
HTTP/2 200
server: nginx
date: Fri, 22 Nov 2019 09:42:59 GMT
content-type: image/jpeg
content-length: 44491
last-modified: Fri, 22 Nov 2019 09:40:03 GMT
accept-ranges: bytes
cache-control: max-age=2592000
expires: Sun, 22 Dec 2019 09:42:59 GMT
via: 10035
aikaiyuan: HIT
RewriteRule wp-content/.*/[^/]*(responsive|css|js|dynamic|loader|fonts)\.php - [E=cache-control:max-age=3600]

禁止某些 IP 访问

order deny,allow
deny from 1.1.1.1   # 1.1.1.1/32
deny from 1.1.1     # 1.1.1.0/24
deny from 1.1       # 1.1.0.0/16
deny from 1         # 1.0.0.0/8

返回 403 错误

禁止某些 User-Agent 访问

SetEnvIfNoCase User-Agent "Mozilla/5.0 \(Windows NT 6.1; WOW64; rv:7.0.1\) Gecko/20100101 Firefox/7.0.1" bad_ua
SetEnvIfNoCase User-Agent "Apache-HttpClient" bad_ah
SetEnvIfNoCase User-Agent "scrapy.org" bad_scrapy

order deny,allow
deny from env=bad_ua
deny from env=bad_ah
deny from env=bad_scrapy

只要包含 就返回 403 错误
注意: 特殊符号转移
测试: curl -A "scrapy.org" https://www.aikaiyuan.com/

禁止某些 URL 访问

SetEnvIf Request_URI /xmlrpc.php deny_xmlrpc
order deny,allow
Deny from env=deny_xmlrpc

返回 403 错误

禁止某些 目录 访问

SetEnvIf Request_URI "test/" deny_dir
Order deny,allow
Deny from env=deny_dir

返回 403 错误

http 调整到 https

RewriteEngine on  
RewriteCond %{HTTP:X-FORWARDED-PROTO} !^https$  
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=307] 

将所有的请求转发到index.php处理

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]  
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php [L] 

将/aa/路径的请求转发到aa目录下的index.php处理,其他转到根目录下的index.php处理

RewriteEngine On  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_URI} ^/aa/  
RewriteRule ^aa/(.*)$ aa/index.php/$1 [L]  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_URI} !^/aa/  
RewriteRule ^(.*)$ index.php/$1 [L]   

转载请注明:爱开源 » htaccess 常用案例

与本文相关的文章

  • 暂无相关文章!
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址