是否有可能使用绝对路径进行 htaccess 301 重定向?

Is there any possibility to have htaccess 301 Redirects with absolute paths?

我想知道是否有任何方法可以像

那样进行重定向
Redirect 301 https://www.mypage.com/test https://www.mypage.com

作为背景:我需要这个,因为我有一个包含 3 种不同语言的网站,并且每种语言在不同的域上运行。如果我使用相对路径进行 /test,它将影响我的每个域,但我只想对一个特定域进行重定向。

我按照我在示例中展示的那样尝试了它,但它不再工作了。
我也在为我的 apache 指令尝试使用 RewriteCond,但它也不适用于绝对路径

在这种情况下你需要写一个RewriteCond,如下例:

RewriteEngine On
RewriteCond %{REQUEST_URI} /test1
RewriteRule (.*) https://example.com/test1/ [R=301,L]
RewriteCond %{REQUEST_URI} /test2
RewriteRule (.*) https://example.com/test2/ [R=301,L]

Redirect 301 https://www.mypage.com/test https://www.mypage.com

mod_alias Redirect 指令仅匹配 URL-path。

您需要使用mod_rewrite在条件RewriteCond 指令)。例如:

RewriteEngine On

RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^test$ https://www.example.com/ [R=302,L]

RewriteRule模式(第一个参数)是一个仅匹配URL-path的正则表达式(类似于Redirect指令,除了在 .htaccess).

中使用时没有斜杠前缀

以上将发出 302(临时)重定向,从 https://www.example.com/test(HTTP 或 HTTPS)到 https://www.example.com/

如果您要重定向到相同的主机名,那么您不一定需要在 substitution 字符串中包含方案+主机名(RewriteRule 的第二个参数指示)。比如下面和上面一样*1:

RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^test$ / [R=302,L]

(*1 除非你在服务器配置中设置了 UseCanonicalName On 并且 ServerName 设置为不是请求的主机名。)

请注意,上面的匹配 www.example.com 完全匹配(CondPattern 上的 = 前缀运算符使其成为词典字符串比较)。

要匹配 example.comwww.example.com(带有可选的尾随点,即 FQDN),请改用正则表达式。例如:

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^test$ https://www.%1/foo [R=302,L]

其中 %1 是对前面 CondPattern 中第一个捕获组的反向引用(即 example.com)。因此,以上将重定向 https://example.com(或 www.example.com)并重定向到 https://www.example.com/foo(总是 www)。

参考: