微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

防止使用url重写规则重定向管理目录

如何解决防止使用url重写规则重定向管理目录

以下设置非常有效,但是如果我尝试转到https://example.com/quotes/wp-admin,它将重定向https://example.com/wp-admin,这不是我想要的,因为现在我再也无法登录{{1 }} WordPress网站

我尝试将其添加 quotes /quotes/中,但无济于事:

.htaccess

我还尝试将以下行添加RewriteCond %{REQUEST_URI} !^/(wp-admin/.*)$ 文件夹中的.htaccess文件中,但这也没有用。

wp-admin

RewriteEngine Off 文件

.htaccess

# 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

解决方法

我尝试将其添加到引号.htaccess中,但无济于事:

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

REQUEST_URI服务器变量包含完整的URL路径,因此应为/quotes/wp-admin(即正则表达式!^/quotes/wp-admin-故意省略尾部斜杠)。因此,上述异常将失败,并且重定向仍将发生。

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

例如,在/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}$-假定文件扩展名由一个点组成,后跟2到4个字符,范围为​​a-zA-Z0-9或{{1} }(下划线)和URL路径的末尾。 _是代表该字符范围的速记字符类。

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

!\.\w{2,4}$

文件系统检查(例如# 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] )相对“昂贵”,因此,如果不是严格必要的话,最好避免使用。

我还尝试将以下行添加到wp-admin文件夹中的.htaccess文件中,但它也无法正常工作。

-f

这应该有效-至少防止RewriteEngine Off 触发的重定向。 (尽管这样也可以防止任何重写,所以可能不理想吗?)

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

使用302(临时)重定向进行测试,直到所有操作均按预期进行,以避免潜在的缓存问题。


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

.htaccess

小点,但您在此处引用了# 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] ,但在woff2文件中引用了woff。理想情况下,这些应该相同。您可以对此进行一般化,如上文针对/quotes/.htaccess文件所述。例如:

/quotes/.htaccess

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。