重定向子文件夹并向生成的 url 添加查询字符串

发布于 2025-01-13 20:04:02 字数 717 浏览 1 评论 0原文

我已将 WordPress 安装从子文件夹移动到域根目录。 我已通过 .htaccess 成功重定向该子文件夹,但我完全无法向其中添加查询字符串,因此我知道客户端何时来自旧链接,同时保留请求所具有的任何先前查询字符串。

我在 .htaccess 文件中的 wordpress 指令之后的(唯一)代码是:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule (.*)$ https://nbek.org/$1?sublog=nox [R=301,QSA,L]
</IfModule>

我也尝试过:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule ^(.*)$ $1?sublog=nox [QSA]
RewriteRule (.*)$ https://nbek.org/$1 [R=301,L]
</IfModule>

根本没有成功。我做错了什么?

I've moved a wordpress installation from a subfolder to the domain root.
I've redirected that subfolder successfuly via .htaccess but I'm completely unable to add a query string to it so I know when the client is coming from an old link while keeping any previous query string the request had.

The (only) code I have in the .htaccess file after the wordpress directives is:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule (.*)$ https://nbek.org/$1?sublog=nox [R=301,QSA,L]
</IfModule>

I've also tried:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule ^(.*)$ $1?sublog=nox [QSA]
RewriteRule (.*)$ https://nbek.org/$1 [R=301,L]
</IfModule>

With no success at all. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-01-20 20:04:02

我在 .htaccess 文件中的(唯一)代码在 wordpress 之后
指令
是:

;
重写引擎开启    
RewriteCond %{HTTP_HOST} ^example.com/blog$ [或]
RewriteCond %{HTTP_HOST} ^example.com/blog/$
RewriteRule (.*)$ https://example.com/$1?sublog=nox [R=301,QSA,L]

如注释中所述,这些指令不能执行任何操作,因为条件RewriteCond指令)永远不会匹配。 HTTP_HOST 服务器变量包含 Host HTTP 请求标头的值 - 不包含 URL 路径。因此,example.com/blog 永远不会匹配。

您还将这些指令放在了错误的位置,它们需要放在 WordPress 代码块之前,而不是“之后”。通过将此规则放置在“Wordpress 指令之后”,除非 /blog 仍然作为物理目录存在,那么(再次)这些指令将永远不会真正执行任何操作,因为它们甚至永远不会被处理(WordPress 代码块捕获请求并将其重写到前端控制器,此时处理实际上停止)。如果 /blog 仍然作为物理目录存在,那么前面的 WordPress 代码块应该忽略该请求,从而允许处理此规则。

我可以向您保证重定向完成得很好。 有点慢但是还好。

可能表明重定向实际上是由 WordPress 本身执行的,而不是 .htaccess。然而,这个“应该”在重定向响应的 HTTP 响应标头中指示。

将所有请求从 /blog 子目录重定向到根目录并包含额外的 URL 参数只是一句简单的话。

例如, WordPress 代码块之前:

# Redirect "/blog/<anything>" to "/<anything>?sublog=nox"
RewriteRule ^blog(?:$|/(.*)) /$1?sublog=nox [QSA,R=301,L]

# BEGIN WordPress
:

或者,通过在重定向中包含方案和主机名(如您所做的那样),确保它始终重定向到 HTTPS 和规范主机名:

RewriteRule ^blog(?:$|/(.*)) https://example.com/$1?sublog=nox [QSA,R=301,L]

这将重定向 /blog/?sublog=nox/blog/foo?bar=1/foo?sublog=nox&bar=1 (保留初始查询字符串)。

RewriteRule 指令本身检查第一个参数中的 URL 路径(即 ^blog(?:$|/(.*)))。正则表达式中的额外“复杂性”是,这将匹配 /blog (无尾部斜杠)和 /blog/ ,并且仍然保留斜杠前缀不重复的替换字符串。

QSA 标志将初始请求中的原始查询字符串(如果有)附加到替换字符串的末尾。

附加说明:

  • 规则之前不需要任何附加条件RewriteCond 指令)。
  • 您不需要重复 RewriteEngine On 指令,因为这应该已经出现在文件后面的 WordPress 代码块内。
  • 您不需要这些指令的 包装器。这些指令不是可选

参考

Apache 官方文档应该是您的参考资料(尽管文档相当简洁并且有些地方缺少示例):

The (only) code I have in the .htaccess file after the wordpress
directives
is:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^example.com/blog$ [OR]
RewriteCond %{HTTP_HOST} ^example.com/blog/$
RewriteRule (.*)$ https://example.com/$1?sublog=nox [R=301,QSA,L]
</IfModule>

As stated in comments, these directives cannot be doing anything since the conditions (RewriteCond directives) will never match. The HTTP_HOST server variable contains the value of the Host HTTP request header - this does not contain the URL-path. So, example.com/blog can never match.

You've also put these directives in the wrong place, they need to go before the WordPress code block, not "after". By placing this rule "after the Wordpress directives", unless /blog still exists as a physical directory then (again) these directive will never actually do anything since they will never even be processed (the WordPress code block catches the request and rewrites it to the front-controller at which point processing effectively stops). If /blog still exists as a physical directory then the preceding WordPress code block should ignore the request, allowing this rule to be processed.

I can assure you the redirection is done fine. A bit slowly but fine.

This might suggest the redirect is actually being performed by WordPress itself, not .htaccess. However, this "should" be indicated in the HTTP response headers of the redirect response.

To redirect all requests from a /blog subdirectory to the root and include an additional URL parameter is just a one-liner.

For example, before the WordPress code block:

# Redirect "/blog/<anything>" to "/<anything>?sublog=nox"
RewriteRule ^blog(?:$|/(.*)) /$1?sublog=nox [QSA,R=301,L]

# BEGIN WordPress
:

Or, ensure that it always redirects to HTTPS and the canonical hostname by including the scheme and hostname in the redirect (as you have done):

RewriteRule ^blog(?:$|/(.*)) https://example.com/$1?sublog=nox [QSA,R=301,L]

This will redirect /blog to /?sublog=nox and /blog/foo?bar=1 to /foo?sublog=nox&bar=1 (preserving the initial query string).

The RewriteRule directive itself checks the URL-path in the first argument (ie. ^blog(?:$|/(.*))). The additional "complexity" in the regex is that this will match both /blog (no trailing slash) and /blog/<anything> and still preserve the slash prefix in the substitution string without duplication.

The QSA flag appends the original query string (if any) from the initial request onto the end of the substitution string.

Additional notes:

  • You do not need any additional conditions (RewriteCond directives) before the rule.
  • You do not need to repeat the RewriteEngine On directive, since this should already occur later in the file, inside the WordPress code block.
  • You do not need the <IfModule> wrapper around these directives. These directives are not optional.

Reference

The official Apache docs should be your go to reference for this (although the docs are rather concise and somewhat lacking examples in places):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文