如何将子域重定向到其他域

发布于 2024-09-03 18:39:05 字数 827 浏览 1 评论 0原文

我试图通过 htaccess mod-rewrite 实现的目标:

使用重写规则将所有子域重定向到新域名。

例如

test1.olddomain.com ==> test1.newdomain.com

test2.olddomain.com ===> test2.newdomain.com

test3.olddomain.com ===> test3.newdomain.com

这是我到目前为止所拥有的,当然是错误的:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [R=301,L]

RewriteRule [a-zA-Z]+\.olddomain.com$ http://$1.newdomain.com/ [R=301,L]

因为我还不是正则表达式迷,所以我需要您的帮助...感谢您在这里提供的任何帮助。我也知道我们可以将前两个条件合二为一。

注意:我不使用 DNS 重定向所有域的原因是许多目录需要特殊的重写规则才能保持 SEO 的位置​​。

What I'm trying to accomplish with htaccess mod-rewrite:

Redirect all sub-domains to new domain name w rewrite rule.

e.g.

test1.olddomain.com ===> test1.newdomain.com

test2.olddomain.com ===> test2.newdomain.com

test3.olddomain.com ===> test3.newdomain.com

This is what I have so far which of course is wrong:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [R=301,L]

RewriteRule [a-zA-Z]+\.olddomain.com$ http://$1.newdomain.com/ [R=301,L]

Since I'm not a Regular Expression junkie just yet, I need your help... Thanks for any help you can give here. I know also we can compile these first two conditions into one.

Note: The reason I don't redirect all domain using DNS is that a lot of directories need special rewrite rules in order to maintain positions on SEO.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2024-09-10 18:39:05

在 .htaccess 文件中,RewriteRules 匹配的“URL”已删除域名和通向当前目录的任何目录。 (在 .htaccess 文件中使用 mod_rewrite 是一个巨大的痛苦;如果您有权访问服务器配置,请在那里执行它!!)

因此,假设您的 .htaccess 位于 DocumentRoot 中,请尝试如下操作:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]

%1 应该是匹配 RewriteCond 中的第一组,并且 $1 应该匹配 URL 部分。

In .htaccess files, the "URL" that RewriteRules match has been stripped of the domain name and any directories that led to the current directory. (Using mod_rewrite in .htaccess files is a huge pain; if you have access to the server conf do it there instead!!)

So, assuming that your .htaccess is in your DocumentRoot, try something like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]

The %1 is supposed to match the first group in the RewriteCond and the $1 is supposed to match the URL part.

池予 2024-09-10 18:39:05
RewriteRule ^(.+)\.olddomain\.com$ http://$1.newdomain.com/ [R=301,L]

您需要在开头指定 ^ 来要求正则表达式引擎匹配从那里开始的行。接下来,匹配“.olddomain.com”之前的任何内容,并将其分配给第一个匹配的模式(稍后可以在 $1 中访问)。您需要用括号 (.+) 括起来,以便将匹配项分配给 $1。

RewriteRule ^(.+)\.olddomain\.com$ http://$1.newdomain.com/ [R=301,L]

You need to specify the ^ at the beginning to ask the regex engine to match a line beginning there. Next, you match anything before ".olddomain.com" and assign that to the first matched pattern (which will later be accessible in $1). You need to surround with parentheses (.+) in order for the match to be assigned to $1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文