Prevent admin directory from redirecting using url rewrite rules

僤鯓⒐⒋嵵緔 提交于 2020-12-13 03:45:27

问题


The below setup works very well but if I try to go to https://example.com/quotes/wp-admin it redirects me to https://example.com/wp-admin which isn't what I want to happen because now I can never log into the /quotes/ WordPress website.

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

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

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

RewriteEngine Off

Root .htaccess file:

# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]


# 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]


# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


# END WordPress

/quotes .htaccess file

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|webp|gif|svg|ttf|woff)$
RewriteRule (.*) /$1 [R=301,L]


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

回答1:


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]


来源:https://stackoverflow.com/questions/64605388/prevent-admin-directory-from-redirecting-using-url-rewrite-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!