Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

12 Most Useful .htaccess Tricks for WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Are you looking for some useful .htaccess tricks for your WordPress site?

The .htaccess file is a powerful configuration file that allows you to do a lot of neat things on your website.

In this article, we will show you some of the most useful .htaccess tricks for WordPress that you can try right away.

Most Useful .htaccess Tricks for WordPress

What Is the .htaccess File and How to Edit it?

The .htaccess file is an Apache web server configuration file. It is a text file that allows you to define rules for your server to follow for your WordPress website.

WordPress uses the .htaccess file to generate an SEO-friendly URL structure. However, this file can do a lot more than store permalink settings.

The .htaccess file is located in your WordPress site’s root folder. You will need to connect to your website using an FTP client or cPanel file manager to edit it.

.htaccess file on a WordPress site

If you cannot find your .htaccess file, then see our guide on how to find the .htaccess file in WordPress.

Before editing your .htaccess file, it is important to download a copy of it to your computer as a backup. You can use that file in case anything goes wrong.

Having said that, let’s take a look at some useful .htaccess tricks for WordPress that you can try:

1. Protect Your WordPress Admin Area

You can use .htaccess to protect your WordPress admin area by limiting access to selected IP addresses only.

Simply copy and paste this code snippet into your .htaccess file:

AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "WordPress Admin Access Control"
AuthType Basic
order deny,allow
deny from all
# whitelist Syed's IP address
allow from
# whitelist David's IP address
allow from

Don’t forget to replace xx values with your own IP address. If you use more than one IP address to access the internet, then make sure you add them as well.

For detailed instructions, see our guide on how to limit access to WordPress admin using .htaccess.

2. Password Protect WordPress Admin Folder

Password protect WordPress admin directory

If you access your WordPress site from multiple locations, including public internet spots, then limiting access to specific IP addresses may not work for you.

You can use the .htaccess file to add an additional password protection to your WordPress admin area.

First, you need to generate a .htpasswds file. You can easily create one by using this online generator.

Upload this .htpasswds file outside your publicly accessible web directory or /public_html/ folder. A good path would be:


Next, create a .htaccess file and upload it in /wp-admin/ directory and then add the following code in there:

AuthName "Admins Only"
AuthUserFile /home/yourdirectory/.htpasswds/public_html/wp-admin/passwd
AuthGroupFile /dev/null
AuthType basic
require user putyourusernamehere
<Files admin-ajax.php>
Order allow,deny
Allow from all
Satisfy any

Important: Don’t forget to replace the AuthUserFile path with the file path of your .htpasswds file and add your own username.

For detailed instructions, see our guide on how to password-protect the WordPress admin folder.

3. Disable Directory Browsing

Disable directory browsing

Many WordPress security experts recommend disabling directory browsing. With directory browsing enabled, hackers can look into your site’s directory and file structure to find a vulnerable file.

To disable directory browsing on your website, you need to add the following line to your .htaccess file:

Options -Indexes

For more on this topic, see our guide on how to disable directory browsing in WordPress.

4. Disable PHP Execution in Some WordPress Directories

Sometimes, hackers break into a WordPress site and install a backdoor. These backdoor files are often disguised as core WordPress files and are placed in the /wp-includes/ or /wp-content/uploads/ folders.

An easier way to improve your WordPress security is by disabling PHP execution for some WordPress directories.

You will need to create a blank .htaccess file on your computer and then paste the following code inside it:

<Files *.php>
deny from all

Save the file and then upload it to your /wp-content/uploads/ and /wp-includes/ directories.

For more information, check out our tutorial on how to disable PHP execution in certain WordPress directories.

5. Protect Your WordPress Configuration wp-config.php File

Probably the most important file in your WordPress website’s root directory is the wp-config.php file. It contains information about your WordPress database and how to connect to it.

To protect your wp-config.php file from unauthorized access, simply add this code to your .htaccess file:

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

6. Setting Up 301 Redirects Through .htaccess File

Using 301 redirects is the most SEO-friendly way to tell your users that content has moved to a new location. If you want to properly manage your 301 redirects on a post-per-post basis, then check out our guide on how to set up redirects in WordPress.

On the other hand, if you want to quickly set up redirects, then all you need to do is paste this code into your .htaccess file:

Redirect 301 /oldurl/
Redirect 301 /category/television/

7. Ban Suspicious IP Addresses

Are you seeing unusually high requests to your website from a specific IP address? You can easily block those requests by blocking the IP address in your .htaccess file.

Simply add the following code to your .htaccess file:

<Limit GET POST>
order allow,deny
deny from
allow from all

Don’t forget to replace xx with the IP address you want to block.

8. Disable Image Hotlinking in WordPress Using .htaccess

Other websites directly hotlinking images from your site can make your WordPress site slow and exceed your bandwidth limit. This isn’t a big issue for most smaller websites. However, if you run a popular website or a website with lots of photos, then this could become a serious concern.

You can prevent image hotlinking by adding this code to your .htaccess file:

#disable hotlinking of images with forbidden or custom image option
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]

This code only allows images to be displayed if the request is originating from or Don’t forget to replace with your own domain name.

For more ways to protect your images, see our guide on ways to prevent image theft in WordPress.

9. Protect .htaccess From Unauthorized Access

As you have seen, there are so many things that can be done using the .htaccess file. Due to the power and control it has on your web server, it is important to protect it from unauthorized access by hackers.

Simply add the following code to your .htaccess file:

<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all

10. Increase File Upload Size in WordPress

There are different ways to increase the file upload size limit in WordPress. However, for users on shared hosting, some of these methods do not work.

One of the methods that has worked for many users is adding the following code to their .htaccess file:

php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300

This code simply tells your web server to use these values to increase file upload size as well as maximum execution time in WordPress.

11. Disable Access to XML-RPC File Using .htaccess

Each WordPress install comes with a file called xmlrpc.php. This file allows third-party apps to connect to your WordPress site. Most WordPress security experts advise that if you are not using any third-party apps, then you should disable this feature.

There are multiple ways to do that. One of them is by adding the following code to your .htaccess file:

# Block WordPress xmlrpc.php requests
<Files xmlrpc.php>
order deny,allow
deny from all

For more information, see our guide on how to disable XML-RPC in WordPress.

12. Blocking Author Scans in WordPress

A common technique used in brute force attacks is to run author scans on a WordPress site and then attempt to crack passwords for those usernames.

You can block such scans by adding the following code to your .htaccess file:

# BEGIN block author scans
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (author=\d+) [NC]
RewriteRule .* - [F]
# END block author scans

For more information, see our article on how to discourage brute force attacks by blocking author scans in WordPress.

We hope this article helped you learn the most useful .htaccess tricks for WordPress. You may also want to see our guide on the most common WordPress errors and how to fix them and our expert pick of the best WordPress plugins to grow your site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

38 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Jackson Andrade says

    I use password protection for wp-login.php. My customers cannot logout when login.php is protected. Is there a way I can allow customers to logout without calling wp-login.php?action=logout?
    Admins too can’t logout, but that’s not an issue.
    Woocommerce customer’s logut url is,
    Both call wp-login.php for logout. Customers are asked for htaccess id and password. If there is a workaround, let me know. Thanks

    • WPBeginner Support says

      If your site has a login for users who are not your admins then we would not recommend password protecting your wp-login.php for the time being and we do not have a workaround at this time.


      • Jackson Andrade says

        Thanks for that Info. Hope WordPress adds a feature in future where it won’t redirect to login.php for logging out.

  3. Abhi says

    Please Help Me.
    when I paste the following code in .htaccess file it shows an error that is..

    It appears you don’t have
    permission to access this page.
    403 Error. Forbidden.

  4. Ben says

    Great article!
    Do I need to do this if I already have installed WordFence plugin?
    Some people don’t recommend messing with .htaccess file.

    • WPBeginner Support says

      None of these tricks are required if you don’t want to use them, they are only helpful tools that you can use.


  5. Sebastian says

    I am not sure what does “Protect .htaccess From Unauthorized Access” mean exactly. Will I be able to access it if I make changes from point 9?

    • WPBeginner Support says

      It means if someone knows where your htaccess is located and tries to view the file by putting that address in the url, the browser will not be able to view it.


  6. reus says

    how to use wp login user name and password (registered user) to access in your no. 2 topic (Password Protect WordPress Admin Folder).

    hope to to find answer here.

    thank you

  7. Selvakumaran Krishnan says

    Hai Syed Balkhi,

    I have to open a URL which has query parameters and strings like this.

    In the above URL, the problem is %3A%2F%2F. It shows 403 forbidden error. If I remove that part, the URL works fine.

    I have searched and tried all the methods like mod rewrite, redirect, etc,. but nothing works.

    Is there any way to remove (or) rewrite (or) redirect that encoded part using .htaccess file. That part is in the middle of so many parameters. There are a lot of query parameters before and after that part.

    Please share your idea.

  8. Kathrine says

    This is a great article!! I followed your instructions and everything works fine. I tried to open my admin site using the different IP address and it works great. Thank you for sharing your knowledge.

  9. Mohamed Adel says

    When Protecting the directory to wp-admin (as explained in 2. Password Protect WordPress Admin Folder), wen I go to any page on the site the massage appears to put password.. How to fix that?
    I tried from Cpanel and the same problem happens

  10. Tony says

    The tip in point 4 for disabling php execution has started to cause issues with the tinymce editor in pages & posts. A php file is included in the tinymce folder that loads the relevant js files. I’ve just removed the htaccess code from the wp-include folder to stop the issue. Maybe there’s another way around this?

  11. Pankaj says

    Point 5 is not working
    (5. Protect Your WordPress Configuration wp-config.php File)

    [05-Mar-2018 08:20:03 Etc/GMT] PHP Parse error: syntax error, unexpected ‘<' in /home/—–/public_html/ on line 91

  12. Maximilian says

    Hi there, thank you!
    Is ist possible to see the whole .htaccess somewhere? Yes, i could read: “put one line after the other” but still I am not sure.

    Is then “# END WordPress° sill the last line or is it somewhere on the top then?

    And what do you think about putting “Options -Indexes” on the very end?

    Thank you for your answer!

  13. yudi cahyadi says

    good article..i have a question, after implementing the code in htaccess. Do I need to install a security plugin or not..??

    yudi cb(beginner)

  14. Kevin says

    Great article and just one question!
    Should you place the extra code (especially speed opimisations) before or after the # BEGIN WordPress part?


  15. Brian Wohn says

    Hi, my theme developer told me this might be in the htaccess, but I don’t know why my wordpress is adding this at the end of all my pages:

    Any idea why its adding the “/?v=8f2564d40946”? I’ve checked my PermaLinks, Slugs, etc and nothing there?

    Thanks for your help!

    • WPBeginner Support says

      Hi Brian,

      It lools like GeoLocation tag added by WooCommerce.

      If you are using WooCommerce, then you can turn it off. Go to WooCommerce General Options page and uncheck option ‘Geolocate with page caching support’ option.


  16. Adrienne Warden says

    Another wonderful post from WP Beginner…Just one tip for all us newbies…While WP Beginner has some of the best tips and trick for WordPress, when it comes to protecting your site, if you are on a shared server, search “support” first. I’ve learned a lot about the backend from reading post on WP Beginner, but the truth of the matter is – I’m no backender and most shared hosting already have a fix in place for these sorts of things…I’m with InMotion and they actual have set up one click solutions for many issues that effect site security. I turned off the file Index right from CPanel…

    Still WP Beginner is my go to for WordPress knowledge…You guys are awesome!

  17. Fien says

    That is a nice article about htaccess. But how to implement this in one file? Can I put all lines after another?

  18. Liew CheonFong says

    Great list. Bookmarked!

    Do you have same list for NGINX web server (which does not read .htaccess file) ?

  19. Pattye says

    There is a way to ban bots from crawling your site the this file. Any suggestions in doing that, besides banning the IP?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.