0

我正在将旧服务器移至 archive.example.com,新服务器将继续在 example.com 上运行,同时所有 www URL 都被规范化为 example.com 或 archive.example.com,并且应该处理尾部斜杠问题.

旧服务器有许多目录,因此所有内容都需要重定向到 archive.example.com,同时保留路径信息,除了将在新服务器上运行的少数目录。我不想重定向并将为新服务器保留的目录是:

/ (根)
/静止的
/博客
/关于

例如:

示例.com => 示例.com
www.example.com => example.com
www.example.com/ => example.com/

example.com/blog => example.com/blog
www.example.com/blog => example.com/blog
www.example.com/blog/ => example.com/blog/

所有其他目录应重定向到 archive.example.com。例如:

example.com/docs => archive.example.com/docs
www.example.com/docs => archive.example.com/docs
www.example.com/docs/ => archive.example.com/docs/

example.com/library/images => archive.example.com/library/images
www.example.com/library/images => archive.example.com/library/images
www.example.com/library/images/ => archive.example.com/library/images/

这是我的 httpd.conf 文件中的内容:


ServerName example.com
ServerAlias www.example.com
UseCanonicalName On

# canonicalize www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ $1 [R=301]

# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^(/|/static|/blog|/about)$
RewriteRule ^/(.*)$ http://archive.example.com/$1  [NC,R=301,L]

这是正确的和/或有更精确的方法吗?

4

2 回答 2

0
RewriteEngine On
RewriteCond %{HTTP_HOST} !^gotactics.net$ [NC]
RewriteRule ^(.*)$ http://gotactics.net/$1 [L,R=301]

这将删除所有 www。如果需要,我相信你也可以改变它。

于 2011-02-27T03:57:25.490 回答
0

我相信我发现了我的问题——它与重定向到旧站点的 RewriteRule 有关。

这是我发布问题时的内容:


# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^(/|/static|/blog|/about)$
RewriteRule ^/(.*)$ http://archive.example.com/$1  [NC,R=301,L]

...我将其改写为:


# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^/$
RewriteCond  %{REQUEST_URI} !^/static.*$
RewriteCond  %{REQUEST_URI} !^/blog.*$
RewriteCond  %{REQUEST_URI} !^/about.*$
RewriteRule ^(.*)$ http://archive.example.com%{REQUEST_URI}  [NC,R=301,L]

这就是为什么。

首先,如您所见,我将单个重写条件分解为四个单独的条件,因为这将使我能够随着新站点的增长而干净地添加更多要排除的目录。

您还会注意到我在 /static、/blog/ 和 /about 之后添加了一个点星号,以便它可以匹配这些目录中的任何路径,而不仅仅是顶级路径。

最后,在 RewriteRule 行上,我从模式中删除了前导斜杠,并将尾随 /$1 更改为 %{REQUEST_URI} 。我不需要在这里存储模式中的任何变量——我只需要更改服务器名称——所以我没有从模式中提取路径,而是通过使用相同的 %{REQUEST_URI} 变量使其更加明确用于前四行。

顺便说一句:起初这让我感到困惑的原因之一是因为 Chrome 有时会缓存 DNS/路径信息——执行 Ctrl-F5 来清除缓存将使您能够看到您的更改。

于 2011-02-27T09:57:46.920 回答