【问题标题】:Rewrite URL for vHost in Apache. [AllowOverride & Mod_rewrite checked]在 Apache 中重写 vHost 的 URL。 [检查允许覆盖和 Mod_rewrite]
【发布时间】:2015-10-23 05:53:05
【问题描述】:

在我的域中将带有 www 的规范 URL 重写为非 www 时遇到问题。 如果我尝试 www.example.com 并且总是得到#404。 这是有关 apache2 配置的所有信息。

这是我检查的所有内容:

/etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
        ServerAdmin info@example.com
        ServerName example.com
        DocumentRoot /var/www/example.com
        <Directory /var/www/example.com>
                AllowOverride All
	        <IfModule mod_rewrite.c>
        	        Options +FollowSymLinks
			RewriteEngine On
			RewriteCond %{HTTP_HOST} ^www.example.com [NC]
			RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
        	</IfModule>
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
	
</VirtualHost>
/etc/apache2/apache2.conf

.
.
.
<Directory />
	Options FollowSymLinks
	AllowOverride All
	Require all denied
</Directory>

<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>

<Directory /var/www/>
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
</Directory>
.
.
.

最后的信息phpinfo()

有什么想法吗? 谢谢!

【问题讨论】:

  • 您确定问题是您认为的问题吗?毕竟,您可能也有一些使用该 url 的源代码。所以,我想你应该检查 htaccess 和你的源代码是否存在潜在问题。另外,看看重写规则。
  • BTW Allowoverride 对您的配置重写规则没有影响,因为您使用 .htaccess 而是在配置中使用重写。因此,为防止不必要的性能损失,请将其关闭。 AllowOverride None。配置更改后您是否也重新启动了 apache?
  • 我尝试过使用 .htaccess,每次更改后我都重新启动了 apache。

标签: .htaccess mod-rewrite apache2 no-www


【解决方案1】:

[已解决]

我刚刚在虚拟主机定义中添加了别名子句,并在 DNS 区域中创建了 CNAME 记录 www.example.com

ServerName example.com
ServerAlias www.example.com

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 2013-02-12
    • 2011-11-10
    • 2016-09-05
    • 2011-06-03
    • 2012-07-14
    • 1970-01-01
    • 2015-10-27
    • 2020-05-14
    相关资源
    最近更新 更多