10个.htaccess代码片段提高WordPress网站安全性
WordPress 虽然是比较安全的程序,但是有些时候管理不当也会有一些安全风险存在的。在这篇文章中,老蒋整理关于基于.htaccsss文件可以处理部分提高WordPress网站安全性的方法。如果我们也有在使用Apache服务器或者虚拟主机的话,可以在.htaccsss中设置安全性。
1、阻止特定IP地址
我们可以直接在.htaccess文件中限制特定的IP地址访问。
Limit GET POST>
order allow,denydeny from IP_ADDRESS_1deny from IP_ADDRESS_2allow from all/Limit>
2、禁用目录浏览
# Disable directory browsingOptions All -Indexes
有些时候主机或者网站的问题,我们是有目录浏览的权限,这样不是特别安全。所以我们可以禁止目录浏览。如果我们是有做资源站提供目录下载的,那也可以开启。
3、只允许指定内容
我们应该知道的,对于在wp-content文件夹包含插件、主题、文件下载图片等。除了禁用目录浏览之外,我们还可以限制特定的内容。 我们可以取消阻止JPG,PDF,DOCX,CSS,JS等文件,并拒绝其余文件。
# Disable access to all file types except the followingOrder deny,allowDeny from allFiles ~ ".(xml|css|js|jpe?g|png|gif|pdf|docx|rtf|odf|zip|rar)$">
Allow from all/Files>
这个文件要新建一个 .htaccsss在wp-content目录中。
4、限制访问wp-includes
# Block wp-includes folder and filesIfModule mod_rewrite.c>
RewriteEngine OnRewriteBase /RewriteRule ^wp-admin/includes/ - [F,L]RewriteRule !^wp-includes/ - [S=3]RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]RewriteRule ^wp-includes/theme-compat/ - [F,L]/IfModule>
5、放行允许的IP访问
# Limit logins and admin by IPLimit GET POST PUT>
order deny,allowdeny from allallow from 302.143.54.102allow from IP_ADDRESS_2/Limit>
6、保护wp-config.php
# Deny access to wp-config.php filefiles wp-config.php>
order allow,denydeny from all/files>
7、设置图片防盗链
# Prevent image hotlinking script. Replace last URL with any image link you want.RewriteEngine onRewriteCond %{
HTTP_REFERER}
!^$RewriteCond %{
HTTP_REFERER}
!^http(s)?://(www\.)?yourwebsite.com [NC]RewriteCond %{
HTTP_REFERER}
!^http(s)?://(www\.)?yourotherwebsite.com [NC]RewriteRule \.(jpg|jpeg|png|gif)$ http://跳转图片 [NC,R,L]
8、开启浏览器缓存
# Setup browser cachingIfModule mod_expires.c>
ExpiresActive OnExpiresByType image/jpg "access 1 year"ExpiresByType image/jpeg "access 1 year"ExpiresByType image/gif "access 1 year"ExpiresByType image/png "access 1 year"ExpiresByType text/css "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 2 days"/IfModule>
9、跳转维护页面
# Redirect all traffic to maintenance.html fileRewriteEngine onRewriteCond %{
REQUEST_URI}
!/maintenance.html$RewriteCond %{
REMOTE_ADDR}
!^123\.123\.123\.123RewriteRule $ /maintenance.html [R=302,L]
10、自定义404页面
# Custom error page for error 403, 404 and 500ErrorDocument 404 /error.htmlErrorDocument 403 /error.htmlErrorDocument 500 /error.html
以上10个功能,我们如果有在使用.htaccess文件提高WordPress安全的,可以用到上面的功能。
参考来自:https://www.wpexplorer.com/htaccess-wordpress-security/
扩展阅读 - htaccess相关的问题:
1、通过.htaccess设置WordPress防盗链的功能
2、几个实用在Apache引擎的.htaccess规则控制技巧(实例WordPress程序)
3、Apache环境.htaccess设置301跳转及常用.htaccess技巧整理
4、通过设定.htaccess和nginx.conf禁止恶意User Agent网页爬虫
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 10个.htaccess代码片段提高WordPress网站安全性
本文地址: https://pptw.com/jishu/665795.html