![Apache 重写以删除“www”不起作用(全局)](https://linux22.com/image/638243/Apache%20%E9%87%8D%E5%86%99%E4%BB%A5%E5%88%A0%E9%99%A4%E2%80%9Cwww%E2%80%9D%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%88%E5%85%A8%E5%B1%80%EF%BC%89.png)
我已经采取了Apache 服务器配置全局.htaccess
文件并将其放在我的 Web 服务器上,如下所示/etc/apache2/apache-server-configs.conf
。然后在我的/etc/apache2/apache2.conf
文件中,我在文件末尾得到了以下内容:
# Include of directories ignores editors' and dpkg's backup files,
# see the comments above for details.
# Apache Server Configs
Include apache-server-configs.conf
# Include generic snippets of statements
Include conf.d/
# Include the virtual host configurations:
Include sites-enabled/
文件apache-server-configs.conf
已正确包含,但此代码块无法运行:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
(此块上方有一条RewriteEngine on
语句,几行以上)如果我将其放在单个域的.htaccess
文件中,它会按预期工作。但在全局级别,它什么也不做。我尝试使用进行调试RewriteLog
,当我访问我的网站时,日志文件是空的。
我错过了什么?
Debian 7.5,Apache 2.2.22
编辑于 2014 年 8 月 2 日:添加更多信息
还值得注意的是,如果我包含一个 conf 文件,并在包含的文件中/etc/apache2/apache2.conf
放入任何调用,RewriteRule
没有任何所有这些都能正常工作。conf 文件中的其他内容都可以正常工作,但重写则不行。
test.com虚拟主机文件:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin [email protected]
ServerName test.com
ServerAlias www.test.com
Options -Indexes
<IfModule mpm_itk_module>
AssignUserID bob bob
</IfModule>
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/bob/www/test.com
# Log file locations
LogLevel warn
ErrorLog /home/bob/logs/test.com/error.log
CustomLog /home/bob/logs/test.com/access.log combined
</VirtualHost>
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
#SSLCertificateChainFile /etc/ssl/test.com/ca-bundle
ServerAdmin [email protected]
ServerName test.com
ServerAlias www.test.com
Options -Indexes
<IfModule mpm_itk_module>
AssignUserID bob bob
</IfModule>
DocumentRoot /home/bob/www/test.com
ErrorLog /home/bob/logs/test.com/error.log
CustomLog /home/bob/logs/test.com/access.log combined
</VirtualHost>
答案1
根据Apache 文档,重写条件默认不会被子 Virtualhost 块继承。根据文档,建议可以像这样设置为继承:
RewriteEngine On
RewriteOptions Inherit
您必须在每个需要继承您在主配置文件中设置的重写规则的虚拟主机上进行此项设置。