WordPress htaccess: Essential Redirects & IP Restrictions for Optimal Security

wordpress htaccess

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

The WordPress htaccess file is a configuration file used by Apache to manage server-level  website settings. In WordPress, this file plays an essential role in handling site operations and server resources interaction. 

Where & How to Locate WordPress htaccess?

The htaccess file is usually located in the root directory of your WordPress installation (e.g., public_htmlor the folder where wp-config.php resides). It is a hidden file due to the preceding dot (.), which means that you need to enable the “show hidden files” option in your file manager to view it. 

Key functions of WordPress htaccess file include: 

  • It helps WordPress manage clean URLs, such as example.com/about-us instead of example.com/?page_id=123.
  • You can add security rules to the WordPress htaccess file to block malicious bots, prevent access to sensitive files, and restrict IP addresses. 
  • The file supports URL redirection for SEO purposes or to guide users to updated pages. 
  • You can enable caching to improve website speed and performance. 
  • Configurations for gzip or Brotli compression to reduce page load time. 

WordPress mostly modifies the .htaccess file when you change permalinks or other settings. This is why you should always backup this file before manual edits. 

Benefits of WordPress htaccess for Websites

The WordPress .htaccess file offers a range of advantages for users that enable you to enhance site performance, security, and functionality. Here are the key benefits: 

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

  1. Improved URL structure allows for clean and SEO friendly URLs by enabling WordPress permalink. 
  2. Restricts access to sensitive files (e.g., wp-config.php or htaccess itself).
  3. Supports 301 (permanent) and 302 (temporary) redirects for managing broken links or moved content.
  4. Facilitates HTTP-to-HTTPS redirection for secure browsing.
  5. Redirects non-www to www (or vice versa) to maintain URL consistency.
  6. Enables browser caching to store static elements locally to reduce load times. 
  7. Implements password protection for directories and restricts bots and crawlers from accessing unnecessary resources. 
  8. Customizes error pages (e.g., 404 Not Found) to improve user experience.
  9. Modifies server settings without requiring direct access to the main server configuration file. 
  10. Allows for granular control over specific directories or files within your WordPress installation.

How to Create a WordPress htaccess File

If your WordPress installation does not have an active htaccess file, you can create a new one by following these steps: 

Step 1: Access Your WordPress Directory

Use an FTP client or your web hosting control panel’s file manager to access the public.html root directory or the directory where WordPress is installed. 

Step 2: Create a New File

In the root directory, create a new file named htaccess. Using the CyberPanel dashboard, go to Websites > List Websites> File Manager to create a new file. 

wordpress htaccess - create a new file with CyberPanel

Then go to the new file and name it htaccess.

Step 3: Add Default WordPress Rules

Open the newly created htaccess file using a text editor and paste the default WordPress rules:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

Step 4: Upload the File (if needed)

If you have created a local file, you need to upload it to the WordPress root directory using your FTP client. 

Step 5: Verify Its Functionality

Check your website and navigate thoroughly to see if everything is working properly. 

How to Edit a WordPress htaccess File

Editing the htaccess file allows you to implement custom rules for redirects, security, or performance. Before making any changes to the file, you should ideally download the existing version as a backup in case something goes wrong. 

  1. Use your web hosting panel to locate and open the htaccess file in the root directory. 
  2. Right click on the htaccess file and select the edit option. 
wordpress htaccess - edit a new file with CyberPanel
  1. Insert the necessary rules in the respective sections of the file, such as: 
  • Redirect HTTP to HTTPS:
    RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  • Block an IP Address:
    <IfModule mod_authz_core.c>

Require all granted

Require not ip 123.45.67.89

</IfModule>

  • Enable Browser Caching:
    <IfModule mod_expires.c>

ExpiresActive On

ExpiresDefault “access plus 1 month”

</IfModule>

  1. Save changes and upload the updated file to replace the old one. 
  2. Test your website’s functionality to ensure that the changes were implemented correctly without errors. 

WordPress htaccess Redirects

Here’s a list of important WordPress htaccess redirects that are commonly used:

  1. 301 Redirect (Permanent Redirect)

Redirect a page permanently to a new link

Example:
Redirect 301 /old-page/ http://www.yoursite.com/new-page/

  1. 302 Redirect (Temporary Redirect)

Redirects a page temporarily to a new URL.

Example:
Redirect 302 /old-page/ http://www.yoursite.com/temporary-page/

  1. Force WWW (Non-WWW to WWW)

Redirects non-www version of the site to the www version.

Example:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^yoursite.com [NC]

RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

  1. Non-Force WWW (WWW to Non-WWW)

Redirects the www version of the site to the non-www version.

Example:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]

RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]

  1. Force HTTPS (HTTP to HTTPS)

Redirects HTTP traffic to HTTPS for secure browsing.

Example:
RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  1. Force HTTP (HTTPS to HTTP)

Redirects HTTPS traffic to HTTP (not recommended for most sites due to security concerns).

Example:
RewriteEngine On

RewriteCond %{HTTPS} on

RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  1. Redirect a Specific Page

Redirects an individual page to another URL.

Example:
Redirect 301 /old-page/ http://www.yoursite.com/new-page/

  1. Redirect All Traffic to a Single Page

Redirects all traffic to a single page (useful for maintenance).

Example:
RewriteEngine On

RewriteRule ^(.*)$ http://www.yoursite.com/maintenance/ [R=301,L]

  1. Redirect to the Homepage

Redirects any request to the homepage.

Example:
RewriteEngine On

RewriteRule ^.*$ http://www.yoursite.com/ [R=301,L]

  1. Redirect from HTTP to HTTPS on Subdomains

Redirects all HTTP requests for a subdomain to HTTPS.

Example:
RewriteEngine On

RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^subdomain\.yoursite\.com$

RewriteRule ^ https://subdomain.yoursite.com%{REQUEST_URI} [L,R=301]

IP Restrictions in WordPress Using htaccess

Restricting access to your WordPress website or specific areas like the admin dashboard is a common security practice. Here is how you can do so: 

Restrict Access to the Entire Website

  • To allow only specific IP addresses to access your site:

<IfModule mod_authz_core.c>

    Require ip 123.45.67.89

    Require ip 98.76.54.32

</IfModule>

<IfModule !mod_authz_core.c>

    Order Deny,Allow

    Deny from all

    Allow from 123.45.67.89

    Allow from 98.76.54.32

</IfModule>

  • Replace 123.45.67.89 and 98.76.54.32 with the allowed IP addresses.

Restrict Access to the WordPress Admin Area

  • To restrict access to wp-admin:

<Files wp-login.php>

    <IfModule mod_authz_core.c>

        Require ip 123.45.67.89

    </IfModule>

    <IfModule !mod_authz_core.c>

        Order Deny,Allow

        Deny from all

        Allow from 123.45.67.89

    </IfModule>

</Files>

Block Specific IP Addresses

  • To block certain IPs from accessing your site:

<IfModule mod_authz_core.c>

    Require all granted

    Require not ip 192.168.1.100

</IfModule>

<IfModule !mod_authz_core.c>

    Order Allow,Deny

    Allow from all

    Deny from 192.168.1.100

</IfModule>

  • Replace 192.168.1.100 with the IP address you want to block.

Troubleshooting WordPress htaccess File WordPress IP Restrictions Not Working

IssueDescriptionSolution
Incorrect File Locationhtaccess file is not in the correct directory.Place the file in the WordPress root directory or the directory you want the restrictions to apply to.
Server ConfigurationApache server may not allow htaccess overrides.Ensure AllowOverride All is enabled in the Apache configuration (httpd.conf).
Wrong IP AddressIncorrect IPs used in the htaccess rules.Verify your IP using tools like WhatIsMyIP.
Syntax ErrorsErrors in htaccess can cause rules to fail.Validate your htaccess file for syntax errors using online validators.
Mod_security ConflictsServer security module may block htaccess rules.Contact your hosting provider to adjust mod_security settings if necessary.
Server CacheCached server configurations may prevent changes from taking effect.Clear the server cache to apply updates.
Apache Modules MissingRequired modules like mod_authz_core or mod_rewrite are not enabled.Ensure these modules are enabled in the Apache configuration.
Unverified RestrictionsRules may not be working as expected due to lack of testing.Test restrictions using a VPN or proxy to simulate access from blocked or allowed IP addresses.
Error Logs Not CheckedImportant clues may be missed if server logs are not reviewed.Review server error logs for insights on why the rules are not working.
Other Rule ConflictsConflicts with existing htaccess rules.Simplify the htaccess file by isolating IP restriction rules and testing them independently.

Wrapping Up – WordPress htaccess

WordPress .htaccess is an important file that you should learn to navigate through expertly. Make sure that you have a proper WordPress htaccess file that compiles with all the best practices and is easy to edit and test. Good luck!

Frequently Asked Questions

1. What happens if my WordPress htaccess file is corrupted?

A corrupted .htaccess file can cause your website to break or display server errors (e.g., 500 Internal Server Error). To fix this:
1. Rename the .htaccess file to .htaccess_old.
2. Generate a new .htaccess file by re-saving your permalinks in the WordPress admin under Settings > Permalinks.

2. Can I create a WordPress htaccess file manually?

Yes, if the .htaccess file is missing, you can create it manually:
1. Open a text editor and paste the default WordPress .htaccess rules.
2. Save the file as .htaccess (without any extension).
3. Upload it to your WordPress root directory.

3. What is the htaccess file in WordPress?

The .htaccess file is a configuration file used by Apache web servers to control directory-level settings. In WordPress, it is commonly used to manage permalink structures, redirects, and security rules.

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!