我怎样才能强制“www”?带 Apache2 的子域 +铁轨 + Phusion 乘客?

发布于 2024-08-14 18:08:06 字数 849 浏览 8 评论 0原文

我的客户希望使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-08-21 18:08:06

将其放在您的主 VirtualHost 条目下方:

<VirtualHost *:80>
    ServerName example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

您的主 VirtualHost 应该有一个 ServerName www.example.com 条目,并且没有别名或重定向。此外,此重定向不需要 mod_rewrite。

Put this below your main VirtualHost entry:

<VirtualHost *:80>
    ServerName example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文