【问题标题】:non-www to www redirect htaccess非 www 到 www 重定向 htaccess
【发布时间】:2015-12-31 21:28:04
【问题描述】:

我有一个小问题,我不知道为什么会这样。

我试过了:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

但没有成功。

它将网站重定向自:http://domain.com to http://www.domain.com 但不是来自http://domain.com/sample-page to http://www.domain.com/sample-page

为什么?!

更新:我使用 HHVM 3.6.6。这可能是原因?! .htaccess 的位置在 httpd-app.conf (Bitnami HHVM)

【问题讨论】:

  • 它可能是重复的,但那是针对提问者遗漏斜杠的情况
  • Redirect non-www to www 没有重复。现在这个代码是活跃的。 RewriteCond %{HTTP_HOST} !^www\.重写规则 ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]

标签: .htaccess redirect web


【解决方案1】:

Bitnami 开发者在这里。

如果你想默认应用这个重定向,你可以在“installdir/apache2/conf/bitnami/bitnami.conf”文件中的默认VirtualHost中添加它。

<VirtualHost *:80>
  ServerName app.example.com
  ServerAlias www.app.example.com
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 ...

<VirtualHost *:443>
  ServerName app.example.com
  ServerAlias www.app.example.com
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 ...

请注意,您需要重新启动 Apache 才能应用更改。

installdir/ctlscript.sh restart

您可以使用此链接了解有关如何配置 Apache 的更多信息。

https://wiki.bitnami.com/Components/Apache

希望对你有帮助。

乔塔

【讨论】: