
The .htaccess is an important WordPress core file often used to add, modify, and override server-level configurations, security, and performance parameters.
In most cases, you can resolve server-level operational issues and challenges by simply updating/changing the rules in the .htaccess file. However, many website owners are unaware of this file’s actual potential and miss out on optimizing their servers (and websites) to the fullest.
To help all such WordPressers, I will highlight several interesting things you could accomplish by using the .htaccess file in WordPress.
Note: The rules and configurations mentioned below work with Apache 2.4
What is a .htaccess File?
A .htaccess file is the control room for your website that contains essential rules that govern all communication with your WordPress web hosting server.
In particular, you can use the .htaccess file in WordPress for tasks such as controlling access to website pages and improving security and performance. You can place the .htaccess in any website’s folder to change the behavior of that folder.
Managed WordPress Hosting Starting From $10/Month
Pay just for the resources you use with the pay-as-you-go plans, and get outstanding cloud hosting services with added benefits. No hidden charges.
Precaution
A single misplaced dot (.) can potentially bring your website down. Thus before making any changes to the .htaccess file, back it up to an offsite location. Contact your hosting provider if anything goes wrong or you need assistance.
How to Locate .htaccess in WordPress?
WordPress officially recommends NGINX & Apache for running your sites. If your WordPress site runs on NGINX, you will see no .htaccess file as it doesn’t use this file. But, if your WordPress site is hosted on Apache Web Server, you will find the .htaccess file in the root directory (public_html or www).
To access your .htaccess file, connect your server via FTP Client like FileZilla & go to your application root folder.
In my case, the root path is “/applications/sqzucfcyqb/public_html” .
Also, if you see no .htaccess file, it might be hidden (it doesn’t have any file extension), mainly because the file manager hides it for security reasons.
To access the .htaccess hidden file, go to the server option (in FileZilla) and check the Force Showing Hidden Files option.
The Default WordPress .htaccess File
The default .htaccess file is located in the application root directory & is hidden in most cases but can be accessed via an FTP client like FileZilla.
If you still can’t find the .htaccess file in the root folder, you can create an empty .htaccess file using Notepad (or any text editor of your choice) and save it with the name “.htaccess” on your desktop.
Set “Save as type” to all files and upload the file to the root directory of your WordPress installation via the FTP Client.
Note: Make sure that the file name is NOT named “htaccess” – it’s written as .htaccess with a period (.) at the start.
The default .htaccess file for WordPress looks like this:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L </IfModule> # END WordPress
Default WordPress .htaccess file
You can also create the .htaccess file for WordPress by following the steps below:
- Visit the WordPress Dashboard.
- Click Settings > Permalinks.
- Click the Save Changes button.
Following the above steps will generate the default .htaccess file inside the root directory.
The WordPress default .htaccess file only handles your site’s permalinks. However, you can change this and add additional rules to control how the Apache web server handles operations-related requests.
How to Edit the .htaccess File in WordPress?
To edit the .htaccess file in WordPress, go to your root directory using the file manager provided by your WordPress hosting provider. You may also use an FTP client like FileZilla.
- Log in to your FTP client.
- Navigate to the ‘public_html’ folder and look for the .htaccess file.
- Right-click and click on the ‘View/Edit’ option to open it in your preferred text editor.
- Make the required changes and save the file.
Another way of editing the WordPress .htaccess file is to make a copy in the local system. Once you are done, replace the live version using FTP or file manager.
WordPress .htaccess Redirects
You can use the .htaccess file in WordPress to control website redirects. Below are some frequently used rules that help you set up and control redirections on your WordPress websites.
301 (Permanent) Redirect
A 301 Redirect tells search engines that a URL has been permanently moved to another location. This is not limited to URLs only; you can also redirect a folder, page, or even a complete website. The following snippet will redirect the oldpage.html to newpage.html:
Redirect 301 /oldpage.html https://www.yourwebsite.com/newpage.html
302 (Temporary) Redirect
Unlike 301, the 302 Redirect tells search engines that the redirection is temporary. Using 302 redirect helps you slow down (or even prevent) SERP shuffles.
Add the following line to the .htaccess file:
Redirect 302 /oldpage.html http://www.yourwebsite.com/newpage.html
Force URL to www
The following .htaccess rule in WordPress will force all the visitors on example.com to use www.example.com
RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
Force URL to non-www
The following WordPress .htaccess rule will force all visitors on www.example.com to use example.com
RewriteEngine on RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Force HTTPS
The following rule in the WordPress .htaccess file will force all your visitors to use HTTPS instead of HTTP for all URLs.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Read more: Learn how to add an SSL certificate to a WordPress website.
Force HTTP
The following rule in the .htaccess file forces your visitors to use HTTP instead of HTTPS for all URLs.
RewriteEngine On RewriteCond %{HTTPS} on RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect Domain To Sub-Directory
The following rule will redirect the domain’s root URL to the subdirectory of your choice.
RewriteCond %{HTTP_HOST} ^example.com$ RewriteCond %{REQUEST_URI} !^/sub-directory-name/ RewriteRule (.*) /subdir/$1
Redirect a URL
If two domains serve the same website, the below-mentioned .htaccess rule will redirect one domain to the other.
Redirect 301 / http://www.mynewwebsite.com/
Think You’re Paying Extra Hosting Costs?
Steer clear of what you pay for your hosting needs. The Web Hosting Savings Calculator helps you instantly find out the right cloud hosting solution based on your requirements.
WordPress .htaccess Security Tips
Since WordPress is an open-source platform, its sites remain vulnerable to security breaches. Therefore, users must take proactive steps, such as opting for a credible web host like Cloudways that prioritizes their website security and remain protected against malicious traffic and DDoS attacks.
You can also secure your WordPress directories and files via the .htaccess file. Follow the following rules to secure your WordPress websites.
Protect the .htaccess File
The .htaccess file can potentially control the entire website. So you must also protect your .htaccess from unauthorized users. Restrict access to the .htaccess file by copying/pasting the following snippet into the .htacess file:
<files ~ "^.*\.([Hh][Tt][Aa])"> order allow,deny deny from all satisfy all </files>
Restrict Access to WordPress Admin Panel
If someone gains access to your WordPress admin panel, they can do whatever they want and even shut down your whole website.
To prevent this, you should restrict access to the WordPress admin panel to specific IP(s) only.
- Create another .htaccess file, and paste the following snippet into it.
# Limit logins and admin by IP <Limit GET POST PUT> order deny,allow deny from all allow from xx.xx.xx.xx #whitelist Farhan’s IP Address </Limit>
- Upload it to the “www.yourwebsite.com/wp-admin/” folder.
Now, if a user tries to log in to your site with an unapproved IP address, he will see the following error:
Note: Don’t forget to replace “xx.xx.xx.xx” with your allowed IP address.
You can easily get your IP by visiting “What Is My IP”. If you’ve got more than one moderator, you can also add multiple IPs by using the following variation:
allow from 34.56.78 98.76.54.32 19.82.73.64
Secure Important Files
You can use .htaccess in WordPress to protect important files such as functions.php, wp-config.php, and php.ini, by using the following snippet:
<FilesMatch "wp-config\.php|php.ini|functions\.php"> Order deny,allow Deny from all </FilesMatch>
Protect the wp-config.php File
In WordPress, wp-config.php is the file where the hosting, database, and other important credentials are saved. So you must protect the file from all unauthorized access.
Copy and paste the following lines into the .htaccess file:
<files wp-config.php> order allow,deny deny from all </files>
Protect the wp-content Folder
The wp-content folder contains all the important themes, plugins, media, and cached files, making it the main target of hackers and spammers.
To protect this folder from unauthorized access, create a separate .htaccess file in the wp-content folder. Next, copy and paste the following snippet into the file:
Order deny,allow Deny from all <Files ~ ".(xml|css|jpe?g|png|gif|js)$"> Allow from all </Files>
With the above rule, users would only be able to upload files with the allowed extensions (XML, CSS, JPG, JPEG, PNG, GIF, and JavaScript), denying all other file types.
Protect Include-Only Files
Some areas of the WordPress installation should never be accessible by the average user. The best practice is to block all access to these files, and you can set up the access restrictions by adding the snippet to the .htaccess file.
<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>
Disable PHP Execution
Restricting the execution of PHP code for all or selected directories of your site is a critical WordPress website security practice. Create a .htaccess file inside a folder where you don’t want to run PHP scripts, and add the snippet below.
<Files *.php> deny from all </Files>
Certain WordPress folders such as wp-includes and /wp-content/uploads/ are writable by default. This type of permission allows users to upload media or different file types. It is always recommended to disable PHP execution on these directories.
File Access Restriction
Restricting access to wp-admin is an important requirement, mainly when several team members are involved in website management and updates.
In practical terms, this means that the users cannot access sensitive files such as plugins, themes, and assets folders.
.htaccess is a great way of protecting direct access to edit PHP files of WordPress plugins and themes, making it harder for hackers to inject malicious code. For this, just add the following lines to the file:
RewriteCond %{REQUEST_URI} !^/wp-content/plugins/file/to/exclude\.php RewriteCond %{REQUEST_URI} !^/wp-content/plugins/directory/to/exclude/ RewriteRule wp-content/plugins/(.*\.php)$ - [R=404,L] RewriteCond %{REQUEST_URI} !^/wp-content/themes/file/to/exclude\.php RewriteCond %{REQUEST_URI} !^/wp-content/themes/directory/to/exclude/ RewriteRule wp-content/themes/(.*\.php)$ - [R=404,L]
Script Injection Protection
Script injection is a notorious technique in which the attacker “injects” a malicious piece of code into the website code to extract data or take over the website.
Adding the following snippet in the WordPress .htaccess file can protect your site from such attacks.
Options +FollowSymLinks RewriteEngine On RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR] RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR] RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2}) RewriteRule ^(.*)$ index.php [F,L]
Block IP Address
If someone is abusing your website, continuously spamming, or launching hacking attempts, their IP is visible in the WordPress admin panel. You can block the IP using the .htaccess file and restrict their access to your website.
Simply copy & paste the following snippet into your site’s .htaccess file to block IPs. Remember to replace the sample IP with the spammer’s IP.
<Limit GET POST> order allow,deny deny from 456.78.9 allow from all </Limit>
Once the snippet is in place, the spammer will see the following error message on your site:
Deny Access to Certain Files
Sometimes, you want to restrict access to certain files. And you can do it by using the following .htaccess rule.
<files your-file-name.txt> order allow,deny deny from all </files>
Disable Directory Browsing
Unauthorized access to website files and folders is a major security risk that can potentially bring down the entire site.
You can control the access to website directories by adding the following snippet to your WordPress .htaccess file:
# disable directory browsing Options -Indexes
WordPress .htaccess Rules for Performance
You can use the .htaccess file in WordPress to enhance your website performance. Just copy & paste the relevant snippets in the .htaccess file.
Enable Browser Cache
The browser cache is temporary storage on your system for the files downloaded by your web browser to render websites correctly. These files may include HTML, CSS, JavaScript, images, and other multimedia content.
You can set rules for how long specific files should be cached, and those limits are set based on popular usage. Add the following snippet in the .htaccess file for WordPress to enable browser caching.
<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!) 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-woff2 "access plus 1 month" 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>
Read more: Learn how to Leverage Browser Caching in WordPress to Optimize Page Load Time
Enable Gzip Compression
Gzip is an extremely powerful compression algorithm that locates similar strings within a text file and replaces them temporarily to reduce the overall file size. It is used as an important website page load speed optimization tool.
Gzip compression is often used on the server level, and many hosting providers like Cloudways have it enabled by default. However, if .htaccess is not working, try adding the following snippet to the WordPress .htaccess file or contact your web hosting provider.
<IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
Control/Restrict Image Hotlinking
Image hotlinking can significantly affect bandwidth usage because whenever an external resource requests an image, your server bandwidth is utilized to deliver the image.
To reduce bandwidth consumption because of image hotlinking, you can add the following code snippet to the .htaccess file:
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC] RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
.htaccess for WordPress Multisite (WPMU)
The default .htaccess file for WordPress Multisite is slightly different from the standard WordPress .htaccess file.
Default .htaccess for WPMU Subfolders
If your WordPress Multisite network is subfolders-based, the default .htaccess file should look like this:
# BEGIN WordPress Multisite # Using subfolder network type: https://wordpress.org/support/article/htaccess/#multisite RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] RewriteRule . index.php [L] # END WordPress Multisite
Default .htaccess for WPMU SubDomains
If your WordPress Multisite network is subdomain based, the default .htaccess file should look like this:
# BEGIN WordPress Multisite # Using subdomain network type: https://wordpress.org/support/article/htaccess/#multisite RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^wp-admin$ wp-admin/ [R=301,L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^(wp-(content|admin|includes).*) $1 [L] RewriteRule ^(.*\.php)$ $1 [L] RewriteRule . index.php [L] # END WordPress Multisite
Final Thoughts!
Regarding server configuration, WordPress .htaccess file is among the most critical files on your server. It is often used for configuring your server and securing various website areas. If you think I have overlooked any crucial .htaccess use case, leave a comment below, and I will update this list.
Frequently Asked Questions
Q. What is the .htaccess file in WordPress?
The .htaccess file in WordPress is a server configuration file of Apache Web Servers that you can use to set up redirections, security, and performance optimizations.
Q. How can I edit a .htaccess file in WordPress?
You can edit the WordPress .htaccess file via your web hosting file manager or text editor. Use any FTP client, like FileZilla, and access the root directory. Open the .htaccess file in a text editor to modify and update it.
Q. Does WordPress create a .htaccess file?
Yes, by default, WordPress has at least one .htaccess file. If you can not find the .htaccess file in the root directory, go to the WordPress dashboard > Settings > Permalinks, and click on Save Changes to create a new .htaccess file.
Q. Is .htaccess necessary for WordPress?
Yes. If your WordPress site is running on Apache Web Server, then it’s necessary. But, if your site runs on NGINX, then your WordPress site doesn’t require a .htaccess file.
Q. What is the default .htaccess file for WordPress?
If you are on Apache Web Server, then you can access the default .htaccess file from your root directory (application/public_html or application/www).
Customer Review at
“Beautifully optimized hosting for WordPress and Magento”
Arda Burak [Agency Owner]
Mustaasam Saleem
Mustaasam is the WordPress Community Manager at Cloudways. Where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music.