How to secure your Woocommerce upload directory

Last edited:
October 31, 2023
Reading time:
5 mins
Topic:
.htaccess

/

Blog

/

Security, WooCommerce

/

How to secure your Woocom...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

Hey there! Let’s talk about something important for all you WooCommerce users out there – securing your WooCommerce upload directory.

Now, you might be thinking, “Why should I bother? What’s the worst that could happen?” Well, the truth is, leaving your WooCommerce upload directory unsecured can leave you vulnerable to all sorts of cyber attacks. Hackers can gain unauthorized access to your website and steal sensitive information such as customer data and payment details. Not to mention, they can also inject malicious code into your website, leading to further security breaches and even complete website downtime.

So, what can you do to secure your WooCommerce upload directory? One effective way is to use an .htaccess file. This file can be added to your upload directory and will allow you to disable PHP execution. This will prevent any unauthorized users from executing malicious PHP code and keep your website safe from cyber attacks.

đź’ˇ Tip: To create an .htaccess file, you’ll need to create a new file in a plain text editor (e.g. Notepad). Then, save it as “.htaccess” and make sure to include the period at the beginning of the filename.

1. Locate .htaccess or create a new one in Woocommerce uploads directory

Firstly, we need to locate the htaccess file in the Woocommerce upload directory. If there is no such file in your installation, we’ll have to create a new .htaccess file ourselves. That is going to be a different .htaccess file from the one that exists in your parent website directory. In order to create the file in the uploads directory, navigate to the directory “www.mywebsite.com/wp-content/uploads/woocommerce_uploads”, either by FTP, cPanel, Plesk or any other preferred way. Then create a new empty text file and rename it to “.htaccess”. Get ready to add the snippets presented below in order to protect your Woocommerce uploads directory!

woocommerce upload directory location
woocommerce_uploads directory location

2. Disable PHP Execution in the Upload Directory:

This code will disable PHP execution in your WooCommerce upload directory, preventing malicious PHP scripts from running and potentially compromising your website. This code is recommended for websites that don’t need to run PHP scripts in their upload directory. Note that this has to be in an htaccess file in the uploads directory (www.mywebsite.com/wp-content/uploads/.htaccess).

# Disable PHP execution in the upload directory
 <FilesMatch "\.(php)$"> 
Order allow,deny 
Deny from all
 </FilesMatch>

All you have to do is copy and paste this code into the .htaccess file in your WooCommerce upload directory, and you’re good to go! This will ensure that PHP files cannot be executed in the directory, making it much more difficult for hackers to gain access to your website from upload directory related exploits.

3. Block Access to Certain File Types in the Upload Directory:

The following code will block access to any PHP, Python, Perl, JSP, ASP, HTML, SHTML, shell, or CGI files that may be uploaded to your WooCommerce upload directory. This will prevent any malicious code from executing and potentially causing harm to your website. This code is recommended for websites that don’t need to run scripts in their upload directory and want to block access to known dangerous file types. Note that this has to be in an htaccess file in the uploads directory (www.mywebsite.com/wp-content/uploads/woocommerce_uploads/.htaccess), similarly with the previous snippet.

# Block access to certain file types in the upload directory 
<FilesMatch "\.(php|php3|php4|php5|pl|py|jsp|asp|htm|shtml|sh|cgi)$"> 
Order deny,allow 
Deny from all 
</FilesMatch>

4. Protect Your WooCommerce Uploads Directory in default .htaccess file:

As an additional layer of security, we can also use rewrite rules to protect our WooCommerce upload directory. By adding the following snippet in the default .htaccess file (www.mywebsite.com/.htaccess), we can apply additional protection by blocking access to all file types included in our rule. Feel free to add more file extensions that you want to block.

# Protect your WooCommerce Uploads Directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/uploads/(.*)\.(php|php3|php4|php5|pl|py|jsp|asp|htm|shtml|sh|cgi)$ - [F,L]
</IfModule>
</FilesMatch>

Using the above rewrite rules, we will block access to any PHP, Python, Perl, JSP, ASP, HTML, SHTML, shell, or CGI files that may be uploaded to the directory.

5. Wrapping up the Woocommerce uploads folder protection

So, to sum it up, securing your WooCommerce upload directory is crucial to keeping your website safe from cyber attacks. By adding a new .htaccess file in the uploads directory, or by optimizing your default .htaccess file, you can effectively disable PHP execution and add an extra layer of security to your website. It’s important to keep in mind that while .htaccess files can help secure your WooCommerce upload directory, they are not foolproof and should be used in conjunction with other security measures to keep your website secure. Stay safe out there!

🚀 Suggested Reading: Do you use CSF (ConfigServer Security and Firewall) in your server? If yes, check our guide for blocking Brute Force attacks on your server level.