10个.htaccess代码片段提高WordPress网站安全性

WordPress 虽然是比较安全的程序,但是有些时候管理不当也会有一些安全风险存在的。在这篇文章中,老蒋整理关于基于.htaccsss文件可以处理部分提高WordPress网站安全性的方法。如果我们也有在使用Apache服务器或者虚拟主机的话,可以在.htaccsss中设置安全性。

10个.htaccess代码片段提高WordPress网站安全性

1、阻止特定IP地址

我们可以直接在.htaccess文件中限制特定的IP地址访问。

<Limit GET POST>
order allow,deny
deny from IP_ADDRESS_1
deny from IP_ADDRESS_2
allow from all
</Limit>

2、禁用目录浏览

# Disable directory browsing
Options All -Indexes

有些时候主机或者网站的问题,我们是有目录浏览的权限,这样不是特别安全。所以我们可以禁止目录浏览。如果我们是有做资源站提供目录下载的,那也可以开启。

3、只允许指定内容

我们应该知道的,对于在wp-content文件夹包含插件、主题、文件下载图片等。除了禁用目录浏览之外,我们还可以限制特定的内容。 我们可以取消阻止JPG,PDF,DOCX,CSS,JS等文件,并拒绝其余文件。

# Disable access to all file types except the following
Order deny,allow
Deny from all
<Files ~ ".(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 files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
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 IP
<Limit GET POST PUT>
order deny,allow
deny from all
allow from 302.143.54.102
allow from IP_ADDRESS_2
</Limit>

6、保护wp-config.php

# Deny access to wp-config.php file
<files wp-config.php>
order allow,deny
deny from all
</files>

7、设置图片防盗链

# Prevent image hotlinking script. Replace last URL with any image link you want.
RewriteEngine on
RewriteCond %{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 caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType 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 file
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteRule $ /maintenance.html [R=302,L] 

10、自定义404页面

# Custom error page for error 403, 404 and 500
ErrorDocument 404 /error.html
ErrorDocument 403 /error.html
ErrorDocument 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网页爬虫

本文出处:老蒋部落 » 10个.htaccess代码片段提高WordPress网站安全性 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送