使用 url 重写规则防止管理目录重定向 [英] Prevent admin directory from redirecting using url rewrite rules

查看:64
本文介绍了使用 url 重写规则防止管理目录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下设置效果很好,但如果我尝试转到 https://example.com/quotes/wp-admin,它会将我重定向到 https://example.com/wp-admin 这不是我想要发生的,因为现在我永远无法登录 /quotes/ WordPress 网站.

我尝试将其添加到 quotes .htaccess 但它没有帮助:

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

我还尝试将以下行添加到 wp-admin 文件夹中的 .htaccess 文件,但它也不起作用.

RewriteEngine 关闭

.htaccess 文件:

# 将任何包含语言代码前缀的 URL 重写到子目录重写规则 ^[a-z]{2}/引号%{REQUEST_URI} [L]# 重写对不存在的静态资源的任何请求(在根中)RewriteCond %{REQUEST_FILENAME} !-fRewriteRule \.(css|js|png|jpg|webp|gif|svg|ttf|woff2)$ 引号%{REQUEST_URI} [L]# 开始 rlrssslReallySimpleSSL rsssl_version[3.3.5]<IfModule mod_rewrite.c>重写引擎开启RewriteCond %{HTTP:X-Forwarded-Proto} !httpsRewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/重写规则 ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]</IfModule><IfModule mod_rewrite.c>重写引擎开启重写基数/重写规则 ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则./index.php [L]</IfModule># 结束 WordPress

/quotes .htaccess 文件

# 重定向对/quotes/

解决方案

我尝试将其添加到引号 .htaccess 中,但没有帮助:

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

REQUEST_URI 服务器变量包含完整的 URL 路径,所以这应该是 /quotes/wp-admin(即正则表达式 !^/quotes/wp-admin - 有意省略尾部斜杠).因此,上述异常将失败,重定向仍然会发生.

您还可以将 .php 添加到要避免的扩展列表中.或者甚至排除任何看起来只是具有文件扩展名的请求 - 假设您的页面 URL 没有文件扩展名".

例如在/quotes/.htaccess中:

# 重定向对/quotes/

\.\w{2,4}$ - 这假设文件扩展名由一个点组成,后跟 azAZ0-9_(下划线)和 URL 路径的结尾.\w 是表示该字符范围的速记字符类.

您还可以更进一步,防止任何映射到物理文件的请求被重定向,尽管上述 !^/quotes/wp-admin/!\.\w{2,4}$ 应该已经捕获了一切.例如:

# 重定向对/quotes/

文件系统检查(例如 -f)相对昂贵",因此如果不是绝对必要,最好避免.

<块引用>

我也尝试将以下行添加到 wp-admin 文件夹中的 .htaccess 文件,但它也不起作用.

RewriteEngine 关闭

这应该有效 - 至少可以防止由 .htaccess 触发的重定向.(虽然这也会防止任何重写,所以可能不理想?)

然而,由于这是一个 301(永久)重定向,它将被浏览器(永久)缓存.因此,您需要确保在测试前清除浏览器缓存.

使用 302(临时)重定向进行测试,直到一切正常,以避免潜在的缓存问题.


旁白:在您的根 .htaccess 文件中:

<块引用>

# 重写任何对不存在的静态资源的请求(在根中)RewriteCond %{REQUEST_FILENAME} !-fRewriteRule \.(css|js|png|jpg|webp|gif|svg|ttf|woff2)$ 引号%{REQUEST_URI} [L]

次要问题,但您在此处引用了 woff2,但在您的 /quotes/.htaccess 文件中引用了 woff.理想情况下,这些应该是相同的.您可以对此进行概括,如上文针对 /quotes/.htaccess 文件所述.例如:

# 重写任何对不存在的静态资源的请求(在根中)RewriteCond %{REQUEST_FILENAME} !-fRewriteRule \.\w{2,4}$ 引号%{REQUEST_URI} [L]

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

解决方案

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]

这篇关于使用 url 重写规则防止管理目录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆