.htaccess是否将/subpage重定向到/subpage/wp登录



尝试使用.htaccess规则在第一次访问时通过追加进行wp登录JS检查/wp登录到url,因为在使用密码保护时会干扰Sucuri防火墙。

我已经创建了一个测试子域,试图让htaccess重定向在现场使用之前发挥作用:

RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]

查看此处:testing.no11.ee/protectedpage

不幸的是,这并没有将查询参数添加到url中。我在这里做错了什么访问页面时的预期结果应该是https://testing.no11.ee/protectedpage?/wp-以浏览器url的身份登录。

完全htaccess:

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]

这会检查QUERY_STRING是否设置为protectedpage,但在您的示例中,是/protectedpage的URL路径,而不是查询字符串。

您还需要首先检查查询字符串是否已设置为/wp-login,否则将获得重定向循环。

但是,您也将代码放错了位置。注意代码块之前的WordPress注释-您不应该手动编辑此代码。这个指令还需要在WordPress前端控制器之前执行,否则,它永远不会被处理。

请在# BEGIN WordPress注释标记之前尝试以下操作:

RewriteCond %{QUERY_STRING} !^/wp-login$
RewriteRule ^(protectedpage)/?$ /$1/?/wp-login [R=302,L]

这与请求的URL上的可选尾部斜杠相匹配,但它会重定向以在目标URL中包含尾部斜杠。

(您不需要重复RewriteEngine on指令。(

如果要重定向到scheme+主机名,则无需包含scheme+。$1反向引用只是保存重复并引用匹配的URL路径,即本例中的protectedpage(没有尾部斜杠(。

然而,这个总是重定向并将/wp-login附加到此URL,而不仅仅是";第一次访问"-这真的是你的要求吗?否则,您需要以某种方式区分";第一个";以及";随后的";访问(可能通过检测cookie?(

更新:次要添加:如何改进这一点,将?/wp-login添加到所有以页面/subpage/为父级的URL中,即/subpage/page-1/subpage/page-2将导致/subpage/page-1?/wp-login等?我尝试使用(.*),但这会从url中删除子页面。。。

您可以执行以下操作:

RewriteCond %{QUERY_STRING} !^/wp-login$
RewriteRule ^(subpage/[^/]+)/?$ /$1/?/wp-login [R=302,L]

[^/]+子模式匹配除/之外的任何字符,因此仅匹配第二个路径段,不包括可选的尾部斜线。这与.*类似,但这将捕获所有内容,包括任何尾部斜杠,因此会在重定向的URL中产生双斜杠。

最新更新