我怎样才能强制“www”?带 Apache2 的子域 +铁轨 + Phusion 乘客?
我的客户希望使用 301 重定向在其网站上强制使用“www”子域。因此,“example.com/something”解析为“www.example.com/somthing”等。
我想做的就是简单地将其添加到我的虚拟主机文件中:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.*
DocumentRoot /data/apps/example.com/current/public
RailsBaseURI /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
启用了 mod 重写:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload
我还确保通过以下方式 我目前的努力结果看来基本上是成功的。 Apache 重新启动,一切都按预期工作,除了重写没有发生。因此“www.example.com”和“example.com”都会解析。浏览器在应该重定向到“www.example.com”时却没有。有什么想法吗?我已经尝试重新加载配置并重新启动 apache 几次,但我似乎无法使重写规则发挥作用。我是否通过将它们放置在虚拟主机而不是 .htaccess 文件中来将它们设置在错误的位置?
这里的任何建议都会有用。我完全困惑了。
My clients want to use 301 redirects to force the 'www' subdomain on their sites. So 'example.com/something' resolves to 'www.example.com/somthing' etc.
What I'm trying to do is simply add this to my vhost file:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.*
DocumentRoot /data/apps/example.com/current/public
RailsBaseURI /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
I've also ensured that mod rewrite was enabled via:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload
The outcome of my current efforts basically appears to be successful. Apache restarts and everything works as expected EXCEPT the rewrite isn't happening. So both 'www.example.com' and 'example.com' resolve. The browser doesn't get redirected to 'www.example.com' when it should. Any ideas? I've tried reloading the configuration and restarting apache several times but I can't seem to get the rewrite rules working. Am I setting them up in the wrong place by placing them here in the vhost instead of a .htaccess file?
Any advice here would be useful. I'm completely baffled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放在您的主 VirtualHost 条目下方:
您的主 VirtualHost 应该有一个 ServerName www.example.com 条目,并且没有别名或重定向。此外,此重定向不需要 mod_rewrite。
Put this below your main VirtualHost entry:
You main VirtualHost should have a ServerName www.example.com entry and no aliases or redirects. Also, mod_rewrite is not required for this redirect.