WordPress .htaccess File: How to Create, Edit & Use it? (2024)

Table of Contents
What Is a WordPress .htaccess File? Fastest WordPress Hosting For Developers & Agencies at just $11/Month* Warning! How to Use .htaccess for WordPress and Leverage Its Benefits? 1. Locate the .htaccess File 2. Adjusting File Browser Settings 3. Creating a Backup Benefits of the .htaccess File for Your WordPress Website How to Locate .htaccess in WordPress? The Default WordPress .htaccess File How to Create a .htaccess File? How to Edit the .htaccess File in WordPress? How to Edit .htaccess WordPress Using cPanel How to Edit .htaccess WordPress Using a Plugin WordPress .htaccess Redirects 301 (Permanent) Redirect 302 (Temporary) Redirect Force URL to www Force URL to non-www Force HTTPS Force HTTP Redirect Domain to Sub-Directory Redirect a URL Redirect a Post or Page Redirect Your Entire Site Think You’re Paying Extra Hosting Costs? WordPress .htaccess Security Tips Protect the .htaccess File Restrict Access to WordPress Admin Panel Secure Important Files Protect the wp-config.php File Protect the wp-content Folder Protect Include-Only Files Disable PHP Execution File Access Restriction Script Injection Protection Block IP Address Deny Access to Certain Files Disable Directory Browsing Join 12,000+ Agencies Loving Our Managed WordPress Hosting! WordPress .htaccess Rules for Performance Enable Browser Cache Enable Gzip Compression Control/Restrict Image Hotlinking .htaccess for WordPress Multisite (WPMU) Default .htaccess for WPMU Subfolders Default .htaccess for WPMU SubDomains Performance-Focused WordPress Hosting from $11/Month! Final Thoughts! Frequently Asked Questions Q. What is the .htaccess file in WordPress? Q. How can I edit a .htaccess file in WordPress? Q. Does WordPress create a .htaccess file? Q. Is .htaccess necessary for WordPress? Q. What is the default .htaccess file for WordPress? Q. What is the purpose of the .htaccess file in WordPress? Q. What common directives or rules are used in the WordPress .htaccess file? Q. Are there any security-related settings or configurations in the WordPress .htaccess file? Q. What are some best practices for optimizing the .htaccess file in WordPress? Q. Can I use the .htaccess file to enable or disable certain features or functionalities in WordPress? Q. How do I troubleshoot issues with the .htaccess file affecting my WordPress site? Q. Are there any tools or plugins available to help manage and modify the .htaccess file in WordPress? Share This Article

WordPress .htaccess File: How to Create, Edit & Use it? (1)

The .htaccess is an important WordPress core file often used to add, modify, and override server-level configurations, security, and performance parameters.

In easier words, you can make your site’s addresses look better, automatically send people to new addresses, and even make your site more secure by blocking unauthorized access.

In most cases, you can resolve server-level operational issues and challenges by simply updating/changing the rules in the WordPress .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 assist WordPress users, this guide will explain how to find and edit the .htaccess file within WordPress, including various rule applications.

Note: The rules and configurations mentioned below work with Apache 2.4.

  • What Is a WordPress .htaccess File?
  • How to Use .htaccess for WordPress and Leverage Its Benefits?
  • How to Locate .htaccess in WordPress?
  • The Default WordPress .htaccess File
  • How to Create a .htaccess File?
  • How to Edit the .htaccess File in WordPress?
  • WordPress .htaccess Redirects
  • WordPress .htaccess Security Tips
  • WordPress .htaccess Rules for Performance
  • .htaccess for WordPress Multisite (WPMU)

What Is a WordPress .htaccess File?

A .htaccess file is the control room for your website containing essential rules that govern all communication with your WordPress web hosting server.

You can find the .htaccess files in the main directory of your WordPress installation. WordPress uses these files to handle default tasks like managing redirects and setting up permalink structures.

In particular, you can use the .htaccess file (also known as server configuration files) 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.

Fastest WordPress Hosting For Developers & Agencies at just $11/Month*

Experience blazing fast server speeds with Cloudways LAMP + NGINX hybrid stack. Improve your Core Web Vitals and supercharge your WordPress with Cloudways Hosting.

Try For Free

Warning!

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 if you need assistance.

How to Use .htaccess for WordPress and Leverage Its Benefits?

To use the .htaccess for WordPress, follow these steps:

1. Locate the .htaccess File

The .htaccess file resides in the root directory of your WordPress website. To locate it, navigate to your website’s main folder using a file browser or an FTP client. Keep in mind that the .htaccess file might be hidden by default. Adjust your file browser settings to display hidden files if you don’t see it.

2. Adjusting File Browser Settings

If the .htaccess file isn’t visible in your file browser, you might need to configure the settings to display hidden files. This step ensures you can access and work with the .htaccess file.

3. Creating a Backup

Before making any changes to the .htaccess file, creating a backup of the original file is crucial. This precautionary measure ensures that you can easily revert to the original state if any issues arise during the process.

Benefits of the .htaccess File for Your WordPress Website

Here’s how you can leverage the .htaccess file’s benefits effectively:

BenefitDescription
Customize Permalink StructureCreate user-friendly and search engine-friendly URLs to improve SEO and accessibility for both visitors and search engines.
RedirectsUtilize 301 redirects for a seamless user experience during URL changes or content relocation, maintaining SEO rankings.
Security EnhancementsStrengthen security by adding rules to restrict access to sensitive areas, directories, and files, preventing unauthorized entry.
Prevent HotlinkingSafeguard resources by preventing hotlinking, conserving bandwidth, and ensuring content is accessed only through your site.
Caching and CompressionImplement browser caching and enable compression through .htaccess to speed up page loads, enhancing user experience.
Custom Error PagesCraft custom error pages (e.g., 404) for consistent, user-friendly experiences when visitors encounter errors while navigating.

How to Locate .htaccess in WordPress?

WordPress officially recommends NGINX and 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 an 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 an FTP Client like FileZilla & go to your application’s root folder. In my case, the root path is “/applications/sqzucfcyqb/public_html”.

WordPress .htaccess File: How to Create, Edit & Use it? (2)

  • 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.

WordPress .htaccess File: How to Create, Edit & Use it? (3)

The Default WordPress .htaccess File

The default .htaccess file is located in the application’s root directory and is mostly hidden 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.

WordPress .htaccess File: How to Create, Edit & Use it? (4)

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 OnRewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPress

Default WordPress .htaccess file

How to Create a .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.

WordPress .htaccess File: How to Create, Edit & Use it? (5)

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.

WordPress .htaccess File: How to Create, Edit & Use it? (6)

  • Right-click and click on the ‘View/Edit’ option to open it in your preferred text editor.

WordPress .htaccess File: How to Create, Edit & Use it? (7)

  • 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.

How to Edit .htaccess WordPress Using cPanel

If you prefer an alternative to the FTP method described earlier, you can use cPanel available in your hosting account. Here are the steps to edit the .htaccess file in WordPress using cPanel:

  • Access Control Panel: Log in to your hosting account’s control panel.
  • Open File Manager: Within the control panel, locate and open the “File Manager” tool.
  • Navigate to public_html: In the left-hand navigation menu, click on the “public_html” folder, which is the main directory of your website.
  • Locate WordPress Folder: Within the “public_html” directory, locate and open the “wordpress” folder where your WordPress installation resides.

WordPress .htaccess File: How to Create, Edit & Use it? (8)

  • Find the .htaccess File: Inside the “wordpress” folder, you’ll find the .htaccess file. Right-click on it to initiate the editing process.
  • Edit .htaccess File: Select the option to edit the .htaccess file. A text editor will open within your browser.
  • Add Code: Introduce any code you wish to implement, ensuring that you place it before the line that reads “# BEGIN WordPress.”

By following these steps, you can effectively modify your WordPress site’s .htaccess file using cPanel.

How to Edit .htaccess WordPress Using a Plugin

Another approach to editing the .htaccess file is by using a WordPress plugin such as Htaccess File Editor. Plugins like this let you edit the .htaccess file directly within the WordPress backend.

WordPress .htaccess File: How to Create, Edit & Use it? (9)

Here’s how you can edit your .htaccess file in WordPress using the Htaccess File Editor plugin:

  • Log in to your WordPress admin dashboard.
  • Search for the “Htaccess File Editor” plugin from the WordPress plugin repository.
  • Install and activate the plugin.
  • Within the dashboard, go to “Settings” and then select “WP Htaccess Editor.”
  • As a preliminary step, insert a new line just before the “# BEGIN WordPress” section in the .htaccess file.
  • And that’s it. You can add any code snippets to define your desired rules or configurations.

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 onRewriteCond %{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 onRewriteCond %{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 OnRewriteCond %{HTTPS} offRewriteRule ^(.*)$ 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 OnRewriteCond %{HTTPS} onRewriteRule ^ 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/

Redirect a Post or Page

If you want to redirect a single post or page, you can add the following line of code for any post or page you want to redirect:

Redirect 301 /old-url-slug https://yourdomain.com/new-url-slug

Let’s say you’d like to redirect “https://www.cloudways.com/blog/core-web-vitals/” to “https://www.cloudways.com/blog/core-web-vitals-seo/”. Then, you would add the following line of code in the .htaccess file:

Redirect 301 /core-web-vitals/ https://www.cloudways.com/blog/core-web-vitals-seo/

The old URL will now redirect everyone who clicks on an internal or external link to the new URL.

Redirect Your Entire Site

If you want to redirect your entire website to a new domain, you can add the following code snippet to the .htaccess file in WordPress:

RewriteEngine onRewriteCond %{HTTP_HOST} ^olddummydomain.com [NC,OR]RewriteCond %{HTTP_HOST} ^www.olddummydomain.com [NC]RewriteRule ^(.*)$ https://newdummydomain.com/$1 [L,R=301,NC]

Replace the dummy text in the code above with your old and new domain names.

To make you better understand: Imagine you had a website called “www.oldwebsite.com/about”. With the changes we’ve made, anyone who goes to that link will be taken to the new page at “www.newwebsite.com/about”.

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.

Calculate Now

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. Cloudways Flexible and Autonomous, both are provided with Cloudflare Enterprise, SafeUpdates, and Vulnerability scanner, ensuring a safe online presence.

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,denydeny from allsatisfy 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,allowdeny from allallow 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:

WordPress .htaccess File: How to Create, Edit & Use it? (10)

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,allowDeny 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,denydeny 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,allowDeny 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 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>

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\.phpRewriteCond %{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\.phpRewriteCond %{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 +FollowSymLinksRewriteEngine OnRewriteCond %{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,denydeny from 456.78.9allow from all</Limit>

Once the snippet is in place, the spammer will see the following error message on your site:

WordPress .htaccess File: How to Create, Edit & Use it? (11)

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,denydeny 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 access to website directories by adding the following snippet to your WordPress .htaccess file:

# disable directory browsingOptions -Indexes

Join 12,000+ Agencies Loving Our Managed WordPress Hosting!

Say goodbye to the need for an in-house IT team. Opt for Cloudways and experience lightning-fast hosting, expert 24x7x365 support, and the performance your clients expect from your agency

Try For Free

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 onExpiresDefault "access plus 1 month"# CSSExpiresByType text/css "access plus 1 year"# Data interchangeExpiresByType 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"# HTMLExpiresByType text/html "access plus 0 seconds"# JavaScriptExpiresByType application/javascript "access plus 1 year"# Manifest filesExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"ExpiresByType text/cache-manifest "access plus 0 seconds"# MediaExpiresByType 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 feedsExpiresByType application/atom+xml "access plus 1 hour"ExpiresByType application/rss+xml "access plus 1 hour"# Web fontsExpiresByType 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 a powerful compression algorithm that locates similar strings within a text file and replaces them temporarily to reduce the overall file size.

It functions similarly to Brotli compression. You can check out our Brotli vs. Gzip compression guide for better understanding. Anyway, Gzip 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, add 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 fontsAddOutputFilterByType DEFLATE application/javascriptAddOutputFilterByType DEFLATE application/rss+xmlAddOutputFilterByType DEFLATE application/vnd.ms-fontobjectAddOutputFilterByType DEFLATE application/x-fontAddOutputFilterByType DEFLATE application/x-font-opentypeAddOutputFilterByType DEFLATE application/x-font-otfAddOutputFilterByType DEFLATE application/x-font-truetypeAddOutputFilterByType DEFLATE application/x-font-ttfAddOutputFilterByType DEFLATE application/x-javascriptAddOutputFilterByType DEFLATE application/xhtml+xmlAddOutputFilterByType DEFLATE application/xmlAddOutputFilterByType DEFLATE font/opentypeAddOutputFilterByType DEFLATE font/otfAddOutputFilterByType DEFLATE font/ttfAddOutputFilterByType DEFLATE image/svg+xmlAddOutputFilterByType DEFLATE image/x-iconAddOutputFilterByType DEFLATE text/cssAddOutputFilterByType DEFLATE text/htmlAddOutputFilterByType DEFLATE text/javascriptAddOutputFilterByType DEFLATE text/plainAddOutputFilterByType DEFLATE text/xml# Remove browser bugs (only needed for really old browsers)BrowserMatch ^Mozilla/4 gzip-only-text/htmlBrowserMatch ^Mozilla/4\.0[678] no-gzipBrowserMatch \bMSIE !no-gzip !gzip-only-text/htmlHeader 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 onRewriteCond %{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/#multisiteRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]# add a trailing slash to /wp-adminRewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]RewriteCond %{REQUEST_FILENAME} -f [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^ - [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/#multisiteRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]# add a trailing slash to /wp-adminRewriteRule ^wp-admin$ wp-admin/ [R=301,L]RewriteCond %{REQUEST_FILENAME} -f [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^ - [L]RewriteRule ^(wp-(content|admin|includes).*) $1 [L]RewriteRule ^(.*\.php)$ $1 [L]RewriteRule . index.php [L]# END WordPress Multisite

Performance-Focused WordPress Hosting from $11/Month!

Get the best performance results with the fastest WordPress hosting, where you leave all management worries on us and focus solely on your growth.

Start Today

Final Thoughts!

Regarding server configuration, the 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).

Q. What is the purpose of the .htaccess file in WordPress?

The .htaccess file in WordPress helps regulate server behavior, allowing customization of configuration rules without adjusting global server settings.

Q. What common directives or rules are used in the WordPress .htaccess file?

Common directives in the WordPress .htaccess file include basic permalink structure, redirects, password protection, disabling directory browsing, custom error pages, blocking specific IPs, and enabling GZIP compression.

Q. Are there any security-related settings or configurations in the WordPress .htaccess file?

Yes, the .htaccess file can be used to enhance website security through measures like preventing directory browsing, blocking external access to wp-config files, and limiting WordPress admin area access to specified IPs.

Q. What are some best practices for optimizing the .htaccess file in WordPress?

Best practices for .htaccess file optimization in WordPress include enabling compression, activating browser caching, implementing 301 redirects, securing your .htaccess file, increasing PHP memory limit, and disabling directory browsing.

Q. Can I use the .htaccess file to enable or disable certain features or functionalities in WordPress?

Yes, the .htaccess file can control various WordPress features, particularly for site performance and security enhancements.

Q. How do I troubleshoot issues with the .htaccess file affecting my WordPress site?

Troubleshooting .htaccess file issues involves backing up your file, renaming or deleting the file, creating a new one, inspecting for incorrect rules, debugging specific rules, checking file permissions, and consulting a professional or hosting provider.

Q. Are there any tools or plugins available to help manage and modify the .htaccess file in WordPress?

Yes, plugins like All In One WP Security & Firewall, WP Htaccess Editor, BulletProof Security, and Htaccess by BestWebSoft can facilitate .htaccess file management in WordPress.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Save EXTRA With the Hosting Pricing Calculator!

Get a detailed cost comparison of the leading hosting providers for FREE. Find the optimum hosting solution that optimizes your savings.

Calculate Now

WordPress .htaccess File: How to Create, Edit & Use it? (12)

Sarim Javaid

Sarim Javaid is a Digital Content Producer at Cloudways. He has a habit of penning down his random thoughts and giving words and meaning to the clutter of ideas colliding inside his mind. His obsession with Google and his curious mind add to his research-based writing. Other than that, he's a music and art admirer and an overly-excited person.

WordPress .htaccess File: How to Create, Edit & Use it? (2024)
Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5773

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.