我需要将 URL 从旧的 Drupal 站点重定向到具有不同域名的新 WordPress 站点。 Drupal 网站包含数千个基因测试,但客户只会继续在 Wordpress 网站上提供一百个测试。
在 Wordpress 站点上的 .htaccess 文件中,我创建了重定向并对其进行了全部测试。他们工作得很好。这是一个例子:
Redirect 301 /test/detail/kptn-defect-syndrome-targeted-testing https://www.example.org/genetic_test/kptn-disorder-kptn-targeted-testing/
Redirect 301 /test/detail/herc2-defect-syndrome-targeted-testing https://www.example.org/genetic_test/herc2-disorder-herc2-targeted-testing/
Redirect 301 /test/detail/hemophilia-b-f9-targeted-testing https://www.example.org/genetic_test/hemophilia-b-f9-targeted-testing/
我还创建了一个通配符重定向,以便不再提供的所有其他测试将解析到他们的新实验室页面:
RedirectMatch 301 /test/(.*) https://www.example.org/about-our-lab/
这也运行良好。
我的问题是在 .htaccess 文件中的哪个位置放置将旧域指向新域的重写规则?这一切都发生在 WordPress 生成的指令之前吗? 这是我的重写规则:
#RewriteCond %{HTTP_HOST} ^(?:www\.)oldexample\.org$ [NC]
#RewriteRule ^https://www.newexample.org%{REQUEST_URI} [L,R=301]
我想解决这个问题,这样当我关闭旧的 Drupal 站点并将域指向包含 Wordpress 站点的服务器时,一切都可以正常工作。目前我拥有 WordPress 指令之上的所有重定向。重写规则也会在上面吗?它会在重定向之上吗?
提前非常感谢。以上如有错误,欢迎指正。
...当我关闭旧的 Drupal 站点并将域指向包含 Wordpress 站点的服务器时,一切正常。
如果旧域名和新域名将指向同一主机,那么您需要将刚刚实现的所有使用 mod_alias
Redirect
和 RedirectMatch
指令的重定向转换为使用 mod_rewrite RewriteRule
。
这是因为 mod_rewrite 首先被处理,尽管
.htaccess
文件中的指令有明显的顺序,所以你的 generic 重定向(使用 mod_rewrite)重定向所有剩余的 URL 将首先被处理 - 导致所有的 2 个重定向您的 /test/
网址。好吧,它仍然可以“工作”,但并不理想。
通常,您不应在同一上下文中混合来自 mod_alias 和 mod_rewrite 的重定向,因为由于处理顺序,您可能会收到意外的重定向。
您实施的重定向的另一个潜在问题是您没有检查主机名,因此您不能在新域上拥有以
/test/
开头的 URL。您不需要检查每个规则的主机名,因为它们可以“分组”在一起。
重写规则也会在上面吗?它会在重定向之上吗?
所有这些重定向(使用 mod_rewrite)必须位于 WordPress 代码块之前,否则它们将永远不会被处理。 mod_alias 重定向可能会放置在任何地方。
#RewriteCond %{HTTP_HOST} ^(?:www\.)oldexample\.org$ [NC]
#RewriteRule ^https://www.newexample.org%{REQUEST_URI} [L,R=301]
您在
RewriteRule
pattern(第一个参数)和
substitution字符串(第二个参数)之间缺少一个 space,因此这将无法按写入方式重定向。 您还缺少 CondPattern
中带括号的子模式后面的
?
,假设 www
子域是可选的。将以上几点结合在一起,重定向可以这样写:
# If the "new domain" is already requested then skip the next 102 rules
RewriteCond %{HTTP_HOST} ^(www\.)?newexample\.org [NC]
RewriteRule ^ - [S=102]
RewriteRule ^test/detail/kptn-defect-syndrome-targeted-testing$ https://www.example.org/genetic_test/kptn-disorder-kptn-targeted-testing/ [R=301,L]
RewriteRule ^test/detail/herc2-defect-syndrome-targeted-testing https://www.example.org/genetic_test/herc2-disorder-herc2-targeted-testing/ [R=301,L]
RewriteRule ^test/detail/hemophilia-b-f9-targeted-testing https://www.example.org/genetic_test/hemophilia-b-f9-targeted-testing/ [R=301,L]
# etc....
# Redirect all other tests to generic page
RewriteRule ^test/ https://www.example.org/about-our-lab/ [R=301,L]
# Redirect everything else to new domain (same URL)
RewriteRule ^ https://www.newexample.org%{REQUEST_URI} [R=301,L]
# WordPress directives follow...
您只需要小心计算要跳过的规则数量即可。正如您所说,我在上面使用了 102 个,他们将“继续提供一百个测试”(因此,100 个重定向),再加上随后的 2 个通用重定向。
您不需要重复
RewriteEngine On
指令,因为该指令已经在文件中(在 WordPress 代码块中)出现了
later。无论如何,这是该指令的最后一个实例,因此,如果您重复
RewriteEngine
,这可能会产生误导。或者,您可以在 .htaccess
子目录中拥有第二个
/redirect-old-urls
文件,并首先在内部将旧域的所有请求重写到该子目录。这样做的优点是可以将所有“旧”重定向保留在主 .htaccess
文件之外,而只需对主 .htaccess
文件进行最小的更改。例如:
在根目录中的主
.htaccess
文件中:
# /.htaccess
# Requests for the old domain are internally rewritten to a subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?oldexample\.org [NC]
RewriteRule (.*) redirect-old-urls/$1 [L]
# WordPress directives follow...
然后,在
/redirect-old-urls/.htaccess
文件中:
# /redirect-old-urls/.htaccess
RewriteEngine On
RewriteRule ^test/detail/kptn-defect-syndrome-targeted-testing$ https://www.example.org/genetic_test/kptn-disorder-kptn-targeted-testing/ [R=301,L]
RewriteRule ^test/detail/herc2-defect-syndrome-targeted-testing https://www.example.org/genetic_test/herc2-disorder-herc2-targeted-testing/ [R=301,L]
RewriteRule ^test/detail/hemophilia-b-f9-targeted-testing https://www.example.org/genetic_test/hemophilia-b-f9-targeted-testing/ [R=301,L]
# etc....
# Redirect all other tests to generic page
RewriteRule ^test/ https://www.example.org/about-our-lab/ [R=301,L]
# Redirect everything else to new domain (same URL)
RewriteRule (.*) https://www.newexample.org/$1 [R=301,L]
请注意,
RewriteRule
模式与相对于目录前缀的URL路径匹配,因此它不包括
redirect-old-urls/
。正是由于这个原因,我还更改了最后一个“包罗万象”的规则以使用反向引用而不是 REQUEST_URI
服务器变量,其中包括完整的 URL 路径(包括任意)redirect-old-urls/
URL 路径。任何对子目录的直接请求(如果有)都将被重定向。