如何将子域重定向到其他域
我试图通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .htaccess 文件中,RewriteRules 匹配的“URL”已删除域名和通向当前目录的任何目录。 (在 .htaccess 文件中使用 mod_rewrite 是一个巨大的痛苦;如果您有权访问服务器配置,请在那里执行它!!)
因此,假设您的 .htaccess 位于 DocumentRoot 中,请尝试如下操作:
%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:
The %1 is supposed to match the first group in the RewriteCond and the $1 is supposed to match the URL part.
您需要在开头指定 ^ 来要求正则表达式引擎匹配从那里开始的行。接下来,匹配“.olddomain.com”之前的任何内容,并将其分配给第一个匹配的模式(稍后可以在 $1 中访问)。您需要用括号 (.+) 括起来,以便将匹配项分配给 $1。
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.