I'd like to exclude a path that matches a rule from booting WordPress. The normal way I'd approach this is using the last flag [L]
in a rule before all the others.
To keep things simple in this example, I'll just pretend I want to match a simple path /foo/
.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/?$ - [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
However, this does not work. A few other options are suggested in this older Stack Overflow post, but none of them work (neither for myself nor anyone in the comments of that post).
I did try this rewrite condition instead of the rule:
RewriteCond %{REQUEST_URI} ^/?(foo/.*)$
As well as adding ErrorDocument 401 default
to the end of the .htaccess
document.
I'd like to exclude a path that matches a rule from booting WordPress. The normal way I'd approach this is using the last flag [L]
in a rule before all the others.
To keep things simple in this example, I'll just pretend I want to match a simple path /foo/
.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/?$ - [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
However, this does not work. A few other options are suggested in this older Stack Overflow post, but none of them work (neither for myself nor anyone in the comments of that post).
I did try this rewrite condition instead of the rule:
RewriteCond %{REQUEST_URI} ^/?(foo/.*)$
As well as adding ErrorDocument 401 default
to the end of the .htaccess
document.
3 Answers
Reset to default 2 +50One small hiccup is that you'll need to update RewriteRule ./
in that second to last line. Here is an updated (and tested) snippet for you:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/foo/.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./ /index.php [L]
</IfModule>
# END WordPress
I tested both:
- https://www.domain/foo/
- https://www.domain/foo/bar/
Hope that helps!!
You should use RewriteCond
instead of RewriteRule
. Use this:
RewriteCond %{REQUEST_URI} !/foo/
So, for example, the full code could be like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !/foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The following rule works for me
RewriteRule ^(foo)($|/) - [L]
meaning, any path beginning with foo like /foo/, /foo/bar/ or whatever, leads to the Apache 404 instead of the themes 404; under the assumption that there is no actual directory with that path.
The rule has to be before the last standard line of the WordPress block:
RewriteRule . /index.php [L]
And it doesn't really matter, if it is after RewriteBase /
, it can be before it; it was previously just hastily and sloppily worded on my part.
Which would give you the chance to leave the block between # BEGIN WordPress
and # END WordPress
untouched, if you'd like to do so. By putting
<IfModule mod_rewrite.c>
RewriteRule ^(foo)($|/) - [L]
</IfModule>
in front of # BEGIN WordPress
.
You might want to prevent the path /foo/ from being created in WordPress, this came to mind:
add_filter( 'wp_unique_post_slug', 'no_more_foo_wp_unique_post_slug', 2, 6 );
function no_more_foo_wp_unique_post_slug(
$slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug
) {
if ( $post_parent === 0 ) {
$pattern = '/^foo$/';
$replace = 'bar';
$slug = preg_replace( $pattern, $replace, $slug );
}
return $slug;
}
/foo
, if the page doesn't exist it displays the theme's 404. Basically the rule doesn't preclude Wordpress from being routed to. – Orun Commented Jun 27, 2019 at 16:26/foo
) doesn't exist and the rewrite is not being redirected to any scripts. Try deactivating plugins and/or try a default theme? Edit: I mean, I'm seeing the default error page that's served by Apache and not WordPress. – Sally CJ Commented Jun 27, 2019 at 16:35RewriteRule ^foo/?$ - [L]
and I put it in the exact same place as in the question - below theRewriteEngine On
. But thatIfModule
block is the only content of my.htaccess
file. Could it be a caching issue? Try clearing your browser and site cache, and try a different browser as well, and see if that helps? – Sally CJ Commented Jun 27, 2019 at 20:42apache
config – Orun Commented Jun 28, 2019 at 15:49