• 25/10/2022 • 3 phút đọc • 13 lượt xem

Protect your WordPress Files and Dirs Carefully

Việt Lâm Coder
Việt Lâm Coder
Senior Fullstack & System Architect • Hotline: 0944.71.4545

And I want to say – yes – WordPress is very safe and in ordinary situation, when you use clean WordPress install with the default Twenty Seventeen theme you have nothing to worry about (unless your password is «querty» or «12345678»).

But there are no ordinary situations in life – you may use a plugin with vulnerabily or not good enough hosting — in this case any of security tips matter and could safe your website.

Step 1. Protect wp-config.php


First of all – place your wp-config.php in the directory above your installation folder. Do not worry – WordPress will find it without problems there.

Second – in the same directory create a file named .htaccess with the following content.


<files wp-config.php>
order allow,deny
deny from all
</files>


It may look something like this:
wp-config.php one level above the WordPress install

On the screenshot public_html is the directory with WordPress.

Third – set file permissions (chmod) to 400. You can do it in your FTP/SFTP client usually via right click on file.

wp-config.php chmod to 400

But what means 400 (or 440 and 444 as well) ? It means that nobody can edit this file. You can not even do it in your WordPress admin area (using a plugin for example).

I also recommend to set 444 chmod for every .htaccess file on your website. In addition, the official WordPess codex recommendation is 755 for all directories and 644 for files.

Step 2. Disable directory browsing


Try to open yourdomain/wp-content/plugins URL in your browser and this is something you shouldn’t see:

Disable plugins directory browsing in WordPress by placing empty index.php in plugins folder.

Usually WordPress already includes empty index.php file in /wp-content/plugins/, /wp-content/themes/ and /wp-content/uploads.

But what about all the other directories without index.php, even like /wp-content/uploads/2017?

Simple — just open .htaccess file which is in your WordPress installation folder and insert at the beginning of the file just this single line:


Options -Indexes


Now, when someone tries to access your dirs directly, he will receive «403 Forbidden».

Step 3. A special attention at uploads folder


Well, uploads directory is a very problematic place in WordPress. If it seems like your website is under attack, look into the uploads folder, I suppose you can find something interesting there.

Our task now is to disable PHP-execution there. There are two ways to implement it (I prefer the first way).


# Way 1
# at first we completely disable access to all the files
<Files ~ ".*..*">
Order Allow,Deny
Deny from all
</Files>
# after that add file extensions you want to allow access
<FilesMatch ".(jpg|jpeg|jpe|gif|png|mp4|pdf)$">
Order Deny,Allow
Allow from all
</FilesMatch>



# Way 2
# Kill PHP Execution
<Files *.php>
deny from all
</Files>


No matter what way you choose, you have to create another .htaccess in you uploads directory.

Step 4. /wp-admin/


By the password


In this method, by adding two simple files in your /wp-admin directory you will completely block everything inside it from unauthorized access.

First file is /wp-admin/.htaccess:


AuthType Basic
# Welcome message
AuthName "Hi, this area is protected!"
# Full path to .htpasswd file, you can use getcwd() function to find out it
AuthUserFile /home/rudrastyh.com/public_html/wp-admin/.htpasswd
require valid-user


This is the content of the /wp-admin/.htpasswd file, each line is the user:encrypted_password. To generate the passwords you can use my tool.

[htpasswd-tool]

By the IP


Protection by the IP is better, but it doesn’t fit for me because I work from many different places and sometimes in my trips.

This code should be placed in /wp-admin/.htaccess


<limit GET>
satisfy any
order deny,allow
deny from all
allow from 213.21.33.55
allow from 213.21.34.
# add your own lines with allowed IP addresses here
require valid-user
</limit>


Issue with admin-ajax.php


The interesting thing is that when you block your /wp-admin directory, the admin-ajax.php file will be blocked as well. So, you can not run ajax scripts outside your admin area.

How to avoid this? — use custom ajaxurl instead, as an option you can add ajax.php file in your WordPress directory with the following content in it:

<?php require( dirname(__FILE__) . '/wp-admin/admin-ajax.php' );

Việt Lâm Coder

Việt Lâm Coder

Tác giả bài viết và chuyên gia quản trị website. Nếu bạn gặp khó khăn trong quá trình thực hiện hoặc có nhu cầu thiết kế website, lập trình web app Laravel, hãy liên hệ ngay với tôi qua Hotline / Zalo: 0944.71.4545.

☕ Mời Tác Giả Ly Cà Phê Ấm Lòng

Nếu bài viết này giúp ích cho công việc của bạn, hãy tiếp thêm động lực cho tác giả duy trì kho tri thức bằng một ly cà phê ấm lòng nhé!

Mã QR ủng hộ tác giả
Ngân hàng TMCP Quân Đội (MB Bank)TONG VIET LAM

Có Thể Bạn Quan Tâm

Cách Mở Tab Ẩn Danh Trên Safari MacBook Đơn Giản

Cách mở tab ẩn danh trên Safari MacBook sẽ giúp cho người dùng có thể bảo vệ quyền riêng tư và duyệt web một cách an toà...

Hướng dẫn 5 cách tắt update Windows 11 vĩnh viễn chi tiết nhất

Mặc định Windows 11 sẽ được tự động cập nhật trong nền. Tuy nhiên nếu vì lý do nào đó mà bạn không muốn nhận các bản...

Hướng dẫn trỏ file hosts cho domain trên hệ điều hành Windows

– Làm cách nào để 1 website có thể hoạt động trên Share hosting, VPS, Server mà không cần cấu hình các record DNS cho do...

Kích hoạt Cloudflare Turnstile trong Contact Form 7 chống spam

Cloudflare Turnstile là giải pháp xác thực không dùng CAPTCHA giúp bảo vệ website WordPress khỏi spam một cách nhẹ nhàng...

Cách Sử Dụng DeepSeek R1 Miễn Phí Trong Visual Studio Code Với Cline?

Hãy đối mặt với thực tế: Các trợ lý lập trình AI như GitHub Copilot thì tuyệt vời, nhưng chi phí đăng ký của chúng có th...

Gmail API – Tất cả những gì bạn cần biết - 2025

Nếu bạn đang tìm cách tích hợp Gmail vào các ứng dụng của riêng mình thì Gmail API chính là thứ bạn cần! Gmail là một lự...

Hướng dẫn tạo màn hình desktop ảo bằng Task View trên Surface Windows 11

Việt Lâm Coder sẽ hướng dẫn tạo lập nhiều màn hình desktop ảo bằng Task View trên Surface Windows 11 để sắp xếp các tác...

How to Transfer PuTTY Sessions To Another Windows Machine

his is an user contributed article. PuTTY is a terminal emulator application which can act as a client for the SSH, T...

Hướng dẫn cách kiểm tra cổng đang mở (Check open port) trên linux

Với nhiều năm kinh nghiệm hỗ trợ Khách hàng của Tenten, chúng tôi nhận thấy việc thường xuyên theo dõi kiểm tra hoạt độn...

Kiểm tra tất cả các port mà Cyber Panel đang cần có được mở hay không ?

Để kiểm tra tất cả các port cùng một lúc, bạn có thể sử dụng lệnh: netstat -tulpn | grep -E ':(8090|80|443|21|40110|401...

Hướng dẫn cài đặt KeyHelp Panel

Tóm tắt về KeyHelp Bạn đã có phần cứng tốt nhất cho các dự án Internet của mình nhưng việc quản lý phức tạp, hiệu suất...

Tổng hợp danh sách Control Panel VPS free tốt nhất hiện nay

Control Panel VPS free cung cấp một lựa chọn tuyệt vời cho những người dùng muốn quản lý VPS một cách dễ dàng và hiệ...