当尝试将非www重定向到www时,err_too_many_redirects

在virtuahost apache conf文件中/etc/apache2/sites-available/000-default.conf

我试着将这些代码放在apache conf文件中。

ServerName example.com RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301] 

我也试过了。

  Redirect "/" "http://www.YOUR-DOMAIN.com/"  

也这样做了。

  ServerName example.com Redirect permanent / http://www.example.com/   ServerName www.example.com # real server configuration  

要解决您的问题,您必须了解“NameVirtualHosting”的概念和Ubuntu部署Apache Web服务的特殊方式。

了NameVirtualHost

您的(本地)apache网络服务器可以在同一地址上为不同的网站提供服务,只要它们以不同方式“命名”即可。 Web服务器尝试在您的apache配置中将主机名(例如example.com)与相应的ServernameServerAlias指令进行匹配。 对于每个“网站”,您需要在配置中使用单独的容器; 在您的情况下,您需要一个用于example.com,另一个用于www.example.com。

Apache Webserver和Ubuntu

Ubuntu apache2服务器包确实改变了默认的binyries,配置文件和目录:

  • Apache Webserver二进制文件是/usr/sbin/apache2
  • 服务器根目录是/etc/apache2
  • 主要配置是/etc/apache2/apache2.conf
  • 其他服务器配置转到/etc/apache2/conf.d
  • 模块配置在/etc/apache2/mods-available/
  • VirtualHost站点在/etc/apache2/sites-available/

到目前为止,这么容易; 现在是棘手的一点(对我而言):使用Ubuntu,您可以获得四个命令来启用/禁用模块和(!)VirtualHosts配置:

  • a2ensite / a2dissite
  • a2enmod / a2dismod

使用这些命令,您可以启用已在/etc/apache2/sites-available/配置的实际网站。 至少有一个默认的虚拟主机配置default.conf ,当由a2ensite default启用时, a2ensite default被链接为sites-enabled/000.default.conf

这一切都有效,因为主配置apache2.conf包含这个Include指令:

Include sites-enabled/

让我们变得实用:

删除sites-enabled/所有符号链接,如果您碰巧在已sites-enabled/存储了实际配置文件sites-enabled/将这些文件移动到sites-available (请注意不要覆盖sites-available/现有文件sites-available/而是重命名旧文件)。

现在,您在sites-available /中创建两个新的虚拟主机文件:

 vi /etc/apache2/sites-available/0_www.example.com  ServerName www.example.com DocumentRoot /var/www DirectoryIndex index-www.html  Options FollowSymLinks AllowOverride None   Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all  ErrorLog ${APACHE_LOG_DIR}/error-www-example-com.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access-www-example-com.log combined  vi sites-available/1_example.com  ServerAdmin webmaster@localhost ServerName example.com ErrorLog ${APACHE_LOG_DIR}/error-example-com.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access-example-com.log combined RewriteEngine On RewriteRule ^/(.*) http://www.example.com/$1 [R=302]  

并启用它们,脚本创建从sites-available/sites-enabled/的符号链接:

sudo a2ensite 0_www.example.com

sudo a2enmod rewrite

sudo a2ensite 1_example.com

(重启apache2服务 – service apache2 restartsystemctl restart apache2

 ls -l sites-enabled/ total 0 lrwxrwxrwx 1 root root 30 Jan 8 17:59 example.com -> ../sites-available/example.com lrwxrwxrwx 1 root root 34 Jan 8 17:52 www.example.com -> ../sites-available/www.example.com 

我们需要简单的索引页面

 # cat /var/www/index-non.html 

example.com

This is the NON-www index page # cat /var/www/index-www.html

www.example.com

This is the www.example.com index page.

请记住,apache webserver遇到的第一个指令将是默认的Web容器,因此VirtualHost指令的顺序很重要; 在我们的例子中,如果我调用没有特定主机名或主机名的网络服务器(未配置),我将获得www-index,因为它是第一个virthost指令。

 # No Hostname given curl http://127.0.0.1/ 

www.example.com

This is the www.example.com index page. # index-www curl -H "Host: www.example.com" http://127.0.0.1/

www.example.com

This is the www.example.com index page. # non-www index curl -H "Host: example.com" http://127.0.0.1/ 302 Found

Found

The document has moved here.


Apache/2.2.22 (Ubuntu) Server at example.com Port 80

(请注意使用Host Header变量-H)

要记住,要使NameVirtualHosting正常工作,必须正确配置webserver和webclient。

我希望我的指示可以帮助您解决问题。