使用httpd将www.domain.com和domain.com重定向到subdomain.domain.com

时间:2017-05-23 03:20:48

标签: php apache http redirect mod-rewrite

我想重定向:

情况1. example.comsubdomain.example.com

情况2. www.example.comsubdomain.example.com

我已将这2个代码放在httpd.conf中。只有情境2正在运作,而不是情况1.

<VirtualHost *:80>
     DocumentRoot "/var/www/html/foo"
     ServerName domain.com
     ServerAlias subdomain.example.com
     <Directory "/var/www/html/foo">
                Options Indexes FollowSymLinks Includes
                Options +ExecCGI
                AllowOverride All
                Order allow,deny
                Allow from All
     </Directory>
     Redirect permanent http://example.com/ http://subdomain.example.com/
</VirtualHost>

<VirtualHost *:80>
     DocumentRoot "/var/www/html/foo"
     ServerName www.example.com
     ServerAlias subdomain.example.com
     <Directory "/var/www/html/foo">
                Options Indexes FollowSymLinks Includes
                Options +ExecCGI
                AllowOverride All
                Order allow,deny
                Allow from All
     </Directory>
     Redirect permanent / http://subdomain.example.com/
</VirtualHost>

2 个答案:

答案 0 :(得分:1)

我找到了答案。将example.comwww.example.com重定向到subdomain.example.com,然后只设置subdomain.example.com

的文档根目录
<VirtualHost *:80>
     ServerName example.com
     ServerAlias www.example.com example.com
     Redirect permanent / http://subdomain.example.com/
</VirtualHost>

<VirtualHost *:80>
     DocumentRoot "/var/www/html/foo"
     ServerName subdomain.example.com
     ServerAlias subdomain.example.com
     <Directory "/var/www/html/foo">
                Options Indexes FollowSymLinks Includes
                Options +ExecCGI
                AllowOverride All
                Order allow,deny
                Allow from All
     </Directory>
</VirtualHost>

通过在httpd.conf中使用这些代码,我能够:

  1. example.com重定向到subdomain.example.com

  2. www.example.com重定向到subdomain.example.com

答案 1 :(得分:0)

我想你可以试试这个:

<VirtualHost *:80>
     DocumentRoot "/var/www/html/foo"
     ServerName www.example.com
     ServerAlias example.com

     <Directory "/var/www/html/foo">
                Options Indexes FollowSymLinks Includes
                Options +ExecCGI
                AllowOverride All
                Order allow,deny
                Allow from All
     </Directory>

     Redirect permanent / http://subdomain.example.com/
</VirtualHost>