温馨提示:本文翻译自stackoverflow.com,查看原文请点击:apache - .htaccess redirects on masked domains, only written to take effect when specific domains are in use
.htaccess apache redirect url-rewriting multisite

apache - .htaccess在被屏蔽的域上重定向,仅在使用特定域时才生效

发布于 2020-04-07 11:23:30

我在Apache服务器上设置了WordPress多站点。由于SEO的原因,我想将尽可能多的JS重定向转移到.htaccess文件中的HTTP 301。由于此多站点上的域被屏蔽,因此所有这些域共享一个顶级目录和一个.htaccess文件。因此,我无法使每个重定向都变得简单

Redirect 301 /example/ https://example.com/example/ 

例如,如果我们有一个服务关闭了德克萨斯州奥斯汀市的某个位置,而另一项服务仍然在德克萨斯州奥斯汀市中处于打开状态,并且两个URL都设置为“ example.com/austin/”,那么我将无法接受-all重定向将“ / austin /”发送到一个地方。这产生了大量潜在的冲突。


因此,我想创建仅在使用特定屏蔽域时才生效的重定向。下面是我正在使用的代码,但是它不起作用,请注意,我要确保同时覆盖了尾部斜杠和非尾部斜杠:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^old1$ https://example.com/new1/ [R=301,NC,L]
RewriteRule ^old1/$ https://example.com/new1/ [R=301,NC,L]
RewriteRule ^old12$ https://example.com/new12/ [R=301,NC,L]
RewriteRule ^old12/$ https://example.com/new12/ [R=301,NC,L]
#...

怎么了

查看更多

提问者
AustinLovelace13
被浏览
82
AustinLovelace13 2020-02-04 01:26

我想到了!

我在那边有一条规则可以强制使用斜杠。

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

因为最重要的是重定向,因此使它们无效。我将该规则移至其下方,并对其进行了修复。