我只是想redirect所有不在虚拟主机ServerName
已经提到的子域来redirect到空的子域。 我试图添加这个到我的httpd.conf这个域的所有其他虚拟主机。
<VirtualHost *:80> ServerName *.example.com RedirectPermanent / http://example.com/ </VirtualHost>
空子域的部分(之前加载)读取像这样。
<VirtualHost *:80> DocumentRoot /var/www/example/htdocs ServerName example.com <Directory /var/www/example/htdocs> Options FollowSymLinks AllowOverride All Order Allow,Deny Allow from all </Directory> </VirtualHost>
重启httpd服务之后,当我的浏览器指向abc.example.com
时,我看到了403 Forbidden。 我究竟做错了什么? 我希望没有必要像这个简单任务的其他答案中所描述的基于正则expression式的匹配。
只要在你的虚拟主机configuration文件中的主要块的下面添加一个块。 只需使用通配符*
为子域指定ServerAlias
。 最后,使用RedirectPermanent
指定redirect地址。
Listen 80 <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ServerName example.com <Directory /var/www/html/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/html ServerAlias *.example.com RedirectPermanent / http://example.com/ </VirtualHost>
<VirtualHost *:80> DocumentRoot "/var/www/example" ServerName *.example.org RedirectPermanent / http://example.com/ </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/example/htdocs" ServerName example.org <Directory /var/www/example/htdocs> Options FollowSymLinks AllowOverride All Order Allow,Deny Allow from all </Directory> </VirtualHost>
对于错误403也许你没有设置默认文档,所以它尝试访问文件夹的内容。 对于默认的文件,你可以使用例如
DirectoryIndex index.html Index.htm index.php