使用.htaccess将示例。(us | es)重定向到子域(us | es).example.com

时间:2015-05-09 12:48:43

标签: apache .htaccess mod-rewrite redirect

我最近为我的网站购买了一个新域名,我想将使用旧域名访问的任何人重定向到新域名。

例如,如果他们去了其中任何一个:

<!DOCTYPE html>
<html lang="en" style="">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script src="js/vendor/jquery.svginject.js"></script>
        <script src="js/site.js"></script>
    </head>
    <body style="background-color:#ffffff">
        <img src="img/audio-spectrum-analyzer.svg" data-audiofile="fondspacial.mp3"  width="18" class="svg-inject">           
    </body>
</html>
<iframe style="display:none" frameborder="0" height="0" width="0" src="nothing.html"></iframe>

你会怎么做这个简单的.us | .es - &gt;子域重定向与.htaccess文件?任何规则都应适用于所有网址。

提前致谢。

3 个答案:

答案 0 :(得分:1)

试试这个:

RewriteCond %{HTTP_HOST} example.us$ 
RewriteRule (.*) http://us.example.com [L,R=301]

RewriteCond %{HTTP_HOST} example.es$ 
RewriteRule (.*) http://es.example.com [L,R=301]

答案 1 :(得分:1)

我找到了这个解决方案

RewriteEngine on

RewriteCond %{HTTP_HOST} ^ example\.us$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.us$
RewriteRule ^/?$ "http\:\/\/us\.example\.com" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.es$
RewriteRule ^/?$ "http\:\/\/es\.epys\.com" [R=301,L]

答案 2 :(得分:0)

RewriteCond %{HTTP_HOST} example.(us|es)$ 
RewriteRule (.*) http://%1.example.com [L,R=301]

就像$1引用RewriteRule行中的括号对一样,%1指的是RewriteCond中的那些括号