云企logo

常见 .htaccess 用法

更新日期:2022-04-20  

顶级域名重定向:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

禁止图片盗链:

RewriteEngine on

# Remove the following line if you want to block blank referrer too

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http(s)?://(.+.)?example.com [NC]

RewriteRule .(jpg|jpeg|png|gif|bmp)$ - [NC,F,L]

# If you want to display a "blocked" banner in place of the hotlinked image,

# replace the above rule with:

# RewriteRule .(jpg|jpeg|png|gif|bmp) http://example.com/blocked.png [R,L]

支持WebP图片格式:

RewriteEngine On

RewriteCond %{HTTP_ACCEPT} image/webp

RewriteCond %{DOCUMENT_ROOT}/$1.webp -f

RewriteRule (.+).(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]

压缩文件:

<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>

增加图片视频等静态文件缓存:

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$">

Header set Cache-Control "max-age=2592000"

</FilesMatch>

增加文件压缩:

<ifmodule mod_deflate.c>

AddOutputFilter DEFLATE html xml php js css

</ifmodule>

 

上一篇:js判断pc端还是移动端代码写法 (js判断终端跳转代码)

下一篇:zblog怎么调用某个标签下的文章?
首页