prevent admin directory from redirecting by using url rewrite rules

I tried adding this to the quotes .htaccess but it doesn’t help:

RewriteCond %{REQUEST_URI} !^/(wp-admin/.*)$

The REQUEST_URI server variable contains the full URL-path, so this should be /quotes/wp-admin (ie. regex !^/quotes/wp-admin – trailing slash intentionally omitted). Consequently, the above exception would fail and the redirect would still occur.

You could also add .php to the list of extensions to avoid. Or even exclude any request that simply looks like it has a file extension – assuming your page URLs don’t have “file extensions”.

For example, in /quotes/.htaccess:

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for wp-admin and static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/quotes/wp-admin
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule (.*) /$1 [R=302,L]

\.\w{2,4}$ – This assumes that file extensions consist of a dot followed by between 2 and 4 characters in the range a-z, A-Z, 0-9 or _ (underscore) and the end of the URL-path. \w is a shorthand character class representing that range of characters.

You could also take this a step further and prevent any request that maps to a physical file being redirected, although the above exceptions for !^/quotes/wp-admin/ and !\.\w{2,4}$ should already catch everything. For example:

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for wp-admin, static resources and any other files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/quotes/wp-admin
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /$1 [R=302,L]

File system checks (such as -f) are relatively “expensive”, so are best avoided if not strictly necessary.

I also tried adding the following line to a .htaccess file in the wp-admin folder but it also did not work.

RewriteEngine Off

This should have worked – to at least prevent the redirect triggered by .htaccess. (Although this would prevent any rewrites as well, so may not be desirable?)

However, since this is a 301 (permanent) redirect it will have been cached (persistently) by the browser. So you would need to ensure the browser cache is cleared before testing.

Test with 302 (temporary) redirects until it all works as intended to avoid potential caching issues.


Aside: In your root .htaccess file:

# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif|svg|ttf|woff2)$ quotes%{REQUEST_URI} [L]

Minor point, but you referenced woff2 here, but woff in your /quotes/.htaccess file. Ideally, these should be the same. You could generalise this, as mentioned above for the /quotes/.htaccess file. For example:

# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.\w{2,4}$ quotes%{REQUEST_URI} [L]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top