Search code examples
wordpressapache.htaccess

Block specific URL in htaccess in Wordpress


I am using this code that blocks a URL with the word foo

RewriteEngine On
RewriteRule ^foo - [F]

It works fine, but, how do I make it work with the existing .htaccess code from WP which is below?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Solution

  • It's just a case of putting that rule before the WordPress code block.

    For example:

    # Block all URLs that start with "/foo"
    RewriteRule ^foo - [F]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    

    See also my answer to the following question on the WordPress SE site that goes into detail if you are still having problems: