我不能做这个 301 wordpress 重定向

I can't do this 301 wordpress redirect

我需要将我所有的 250 万个页面从我的网站重定向到一个 URL 添加另一个栏。

例如:

https://www.example.com/my-url-is-here/perfil

https://www.example.com/enterprise/my-url-is-here/perfil

htaccess 或 php header?

因此,您需要在 URL 路径的开头添加 /enterprise。我假设您需要排除以其他方式映射到静态文件(如图像、CSS、JS 和 WordPress 前端控制器 index.php 本身)的请求。在 .htaccess 文件的顶部添加以下内容:

RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule !^enterprise https://www.example.com/enterprise%{REQUEST_URI} [R=301,L]

以上说明,如果请求的 URL-path 不以 /enterprise 开头并且不以看起来像文件扩展名的内容结尾,则重定向,在 URL- 前面加上前缀/enterprise.

路径

我假设所有静态资源都包含标准文件扩展名(.css.js.png 等)并且您的 WordPress URL 不看起来他们没有文件扩展名,例如。不是 .html 等形式。这个简单的模式匹配避免了更昂贵的文件系统检查。

包含完整的规范 URL(即 https://www.example.com/)以避免在请求非规范 URL.

时出现多次重定向

首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。

更新#1:

I do not need to redirect the entire site, but only in URLs ending in /perfil or /perfil/amp

在这种情况下,您可以添加另一个条件来匹配这些 URL。例如:

RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} /perfil(/amp)?$
RewriteRule !^enterprise https://www.example.com/enterprise%{REQUEST_URI} [R=301,L]

更新#2:

there are still some urls that I would like to be redirected by adding / enterprise, but these do not have the termination /perfil or /amp Example: example.com/my-url-is-here and I would like to be redirected to: example.com/enterprise/my-url-is-here/perfil

这些似乎是“特殊情况”,因此您需要为每个“特殊”添加单独的指令URL。

例如,可以将以下内容放在上面的规则之前或之后:

RewriteRule ^my-url-is-here$ https://www.example.com/enterprise/[=12=]/perfil [R=301,L]

[=30=] 是对与 RewriteRule 模式 .

匹配的整个 URL 路径的反向引用

更新#3:

...all urls must necessarily contain the following words /enterprise/my-url-is-here/perfil or /enterprise/my-url-is-here/perfil/amp

如果必须重定向 所有 URL,那么是的,您可以实施单个规则。但是,您显然不能重定向到 .../perfil .../perfil/amp - 您需要决定其中之一。

例如:

RewriteCond %{REQUEST_URI} !^/(enterprise|perfil)(/|$)
RewriteRule ^([\w-]+)/?$ https://www.example.com/enterprise//perfil [R=301,L]

以上重定向所有对单个路径段 URLs 的请求,其中该路径段只能由字符 a-zA-Z0-9_(下划线)或 -(连字符)并重定向到 /enterprise/<url>/perfil.

形式的 URL

它将重定向 /foo/foo/,但不会重定向 /something/foo/foo/something/enterprise/perfil(如果它们可以有效请求?)。

反向引用包含捕获的 URL 路径减去尾部斜杠(如果有)。