RewriteRule:将旧路径重定向到存档服务器并规范化到非 www 域?
我将旧服务器移至 archive.example.com,新服务器将继续在 example.com 上运行,而所有 www URL 均规范化为 example.com 或 archive.example.com,并且应处理尾部斜杠问题。
旧服务器有很多目录,因此除了将在新服务器上运行的几个目录外,所有内容都需要重定向到 archive.example.com,同时保留路径信息。我不想重定向并将保留用于新服务器的目录是:
/ (root) /static /blog /about
例如:
example.com => example.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]
这是正确的和/还是有更精确的方法?
I am moving the old server to archive.example.com, and the new server will continue to run on example.com while all www URLs are canonicalized to either example.com or archive.example.com and should deal with the trailing slash issue.
The old server has many directories so everything needs to redirect to archive.example.com while retaining the path information, except for a few directories which will run on the new server. The directories I do NOT want to redirect and will remain for the new server are:
/ (root) /static /blog /about
For example:
example.com => example.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/
All other directories should redirect to archive.example.com. For example:
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/
Here is what I have in my httpd.conf file:
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]
Is this correct and/or is there a more precise way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将删除所有 www.我相信如果需要的话你也可以改变它做不同的事情。
This will remove all www. Im sure you can change it too do different if needed.
我相信我发现了我的问题——它与重定向到旧站点的 RewriteRule 有关。
这就是我发布问题时所遇到的情况:
...我将其重写为:
这就是原因。
首先,正如您所看到的,我将单个重写条件分解为四个单独的条件,因为这将使我能够随着新站点的增长而干净地添加更多目录以进行排除。
您还会注意到,我在 /static、/blog/ 和 /about 之后添加了一个点星号,以便它可以匹配这些目录中的任何路径,而不仅仅是顶层。
最后,在 RewriteRule 行上,我从模式中删除了前导斜杠,并将结尾的 /$1 更改为 %{REQUEST_URI} 。我不需要在此处存储模式中的任何变量 - 我只需要更改服务器名称 - 因此,我没有从模式中提取路径,而是使用与该模式相同的 %{REQUEST_URI} 变量使其更加明确用于前四行。
顺便说一句:一开始这让我感到困惑的原因之一是 Chrome 有时会缓存 DNS/路径信息 - 使用 Ctrl-F5 清除缓存将使您能够看到所做的更改。
I believe I found my issue -- it was with the RewriteRule that redirected to the old site.
This is what I had when I posted the question:
...and I rewrote this to:
Here's why.
First, as you can see, I broke up the single rewrite condition into four separate conditions because this will enable me to cleanly add more directories for exclusion as the new site grows.
You will also notice that I added a dot-star after /static, /blog/ and /about so that it will match on any path in those directories and not just the top level.
Finally, on the RewriteRule line I removed the leading slash from the pattern and changed the trailing /$1 to %{REQUEST_URI} . I don't need to store any variables from the pattern here -- I just need to change the server name -- so instead of extracting the path from the pattern, I made it more explicit by using the same %{REQUEST_URI} variable that was used on the previous four lines.
BTW: One of the reasons this was causing confusion for me at first was because Chrome was sometimes caching the DNS/path info -- doing a Ctrl-F5 to purge the cache will enable you to see your changes.