2025年3月28日 星期五 甲辰(龙)年 月廿七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 万维网络 > 网站优化

5 个 htaccess 的设置使您的网站更快更安全

时间:12-14来源:作者:点击数:12

htaccess 功能强大,但是设置也极为复杂,多数我们网站的功能都需要依靠这个文件,这篇给大家分享 5 个 htaccess 设置技巧。

阻止访问隐藏文件和目录

我们尝试我们的代码推到生产服务器,而无需隐藏文件和导演,像我们修改系统的董事,但是这并不总是发生。 这段代码可以防止那些文件被访问:

  • <IfModule mod_rewrite.c>
  • RewriteCond %{SCRIPT_FILENAME} -d [OR]
  • RewriteCond %{SCRIPT_FILENAME} -f
  • RewriteRule "(^|/)\." - [F]
  • </IfModule>

黑客不应该得到这些文件,所以现在他们不能的能力!

压缩送达文件按 MIME 类型

有一些我们知道我们想要的出路压缩,并与mod_deflate模块,我们可以直接在服务器这样做的文件类型:

  • <IfModule mod_deflate.c>
  • # Compress all output labeled with one of the following MIME-types
  • # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
  • # and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
  • # as `AddOutputFilterByType` is still in the core directives).
  • <IfModule mod_filter.c>
  • AddOutputFilterByType DEFLATE application/atom+xml \
  • application/javascript \
  • application/json \
  • application/rss+xml \
  • application/vnd.ms-fontobject \
  • application/x-font-ttf \
  • application/x-web-app-manifest+json \
  • application/xhtml+xml \
  • application/xml \
  • font/opentype \
  • image/svg+xml \
  • image/x-icon \
  • text/css \
  • text/html \
  • text/plain \
  • text/x-component \
  • text/xml
  • </IfModule>
  • </IfModule>

我喜欢它是多么容易通过 MIME 类型。通过 htaccess 文件来压缩文件。 少量的代码,大规模增强对所有用户。

允许跨域字体与CORS

我不知道我会如何回应大都有当我第一次张贴了关于从远程 CDN 地址加载 CSS 样式字体 。 他们是为人们很大的,令人困惑的问题,但 HTML5 BP 也有一个解决方案:

  • <IfModule mod_headers.c>
  • <FilesMatch "\.(eot|otf|ttc|ttf|woff)$">
  • Header set Access-Control-Allow-Origin "*"
  • </FilesMatch>
  • </IfModule>

字体是最好的服务关闭的CDN所以现在你可以这样做没有问题!

允许跨域与CORS图片

图片通常是冷静地从不同的域服务,但如果你想访问他们用帆布的数据,你就麻烦了。 这个片段可让您透过画布获得原始图像数据:

  • <IfModule mod_setenvif.c>
  • <IfModule mod_headers.c>
  • <FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$">
  • SetEnvIf Origin ":" IS_CORS
  • Header set Access-Control-Allow-Origin "*" env=IS_CORS
  • </FilesMatch>
  • </IfModule>
  • </IfModule>

之后你得到的图像数据,你可以添加过滤器和更多。

过期失效

Expires 头部都在你的文件设置长缓存过期的真棒方式。 你的静态文件 CSS、图片、JavaScript等,设置长的过期时间可以是一个巨大的性能提升。

  • <IfModule mod_expires.c>
  • ExpiresActive on
  • ExpiresDefault "access plus 1 month"
  • # CSS
  • ExpiresByType text/css "access plus 1 year"
  • # Data interchange
  • ExpiresByType application/json "access plus 0 seconds"
  • ExpiresByType application/xml "access plus 0 seconds"
  • ExpiresByType text/xml "access plus 0 seconds"
  • # Favicon (cannot be renamed!) and cursor images
  • ExpiresByType image/x-icon "access plus 1 week"
  • # HTML components (HTCs)
  • ExpiresByType text/x-component "access plus 1 month"
  • # HTML
  • ExpiresByType text/html "access plus 0 seconds"
  • # JavaScript
  • ExpiresByType application/javascript "access plus 1 year"
  • # Manifest files
  • ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
  • ExpiresByType text/cache-manifest "access plus 0 seconds"
  • # Media
  • ExpiresByType audio/ogg "access plus 1 month"
  • ExpiresByType image/gif "access plus 1 month"
  • ExpiresByType image/jpeg "access plus 1 month"
  • ExpiresByType image/png "access plus 1 month"
  • ExpiresByType video/mp4 "access plus 1 month"
  • ExpiresByType video/ogg "access plus 1 month"
  • ExpiresByType video/webm "access plus 1 month"
  • # Web feeds
  • ExpiresByType application/atom+xml "access plus 1 hour"
  • ExpiresByType application/rss+xml "access plus 1 hour"
  • # Web fonts
  • ExpiresByType application/font-woff "access plus 1 month"
  • ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
  • ExpiresByType application/x-font-ttf "access plus 1 month"
  • ExpiresByType font/opentype "access plus 1 month"
  • ExpiresByType image/svg+xml "access plus 1 month"
  • </IfModule>

你可能会问自己有关更新您的文件和问题的新文件的版本不被更新。 添加一个查询字符串到您的文件的 URL 和文件版本将通过客户端在适当的时候进行下载。

HTML5 的样板是有用的代码一个金矿。 即使你不想包括它的所有项目中,花几分钟检查出的 CSS、htaccess 和 JavaScript 代码吧为您提供,它可以教你的技术能在您的职业生涯随身携带。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
上一篇:新网站pc/wp站点SEO优化建议 下一篇:很抱歉没有了
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐