【发布时间】:2013-04-14 23:52:03
【问题描述】:
我有一台服务器,它使用 Apache 的虚拟主机恶作剧从一个 IP 地址为多个域提供服务。如果 URL 中省略了 www,则其中三个站点需要重定向到。
我在每个域的.htaccess文件中有如下规则:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
这适用于三个中的两个,但第三个完全不遵守。我知道.htaccess 正在被命中,因为框架要求所有命中都通过index.php... 进行路由,而且这是正确的。因此,这不是权限,.htaccess 在每个域上都是相同的(或多或少)。我什至研究过缓存(尽管这没有任何意义......绝望让位于精神错乱!)
如果您有任何线索,请帮助我。
根据要求,这是完整的vhost 配置和.htaccess 文件...
vhost 配置:
<VirtualHost *:80>
ServerAdmin me@gmail.com
DocumentRoot /var/www/example.com
ServerName www.example.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.com>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
.htaccess文件:
# BEGIN example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=40]
####################################################
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
####################################################
# redirect to RoutingHandler
RewriteRule ^.*$ /index.php [NC,L]
</IfModule>
# END example.com
请记住,还有两个其他域以相同的方式设置......它们都可以零问题工作。
【问题讨论】:
-
看起来不错...除了
www问题。我已经竭尽全力确保它提供正确的代码。我知道搞砸虚拟主机配置是多么容易。 -
我什至尝试了明确提到域的正则表达式版本...
^domain\.com$...不走运。只是domain.com,没什么特别的。我完全被难住了。 -
-
嗯……只有蟋蟀吧?
-
我认为问题在于“...还有两个其他域以相同的方式设置...而且它们都可以解决零问题。”,其中意味着
AllowOverride在这些域中也设置为None。但是,Apache 文档包含以下关于 AllowOverride 指令的声明:“当此指令设置为 None 时,.htaccess 文件将被完全忽略。在这种情况下,服务器甚至不会尝试读取 .htaccess 文件在文件系统中。"
标签: apache mod-rewrite virtualhost