.htaccess 非www到www重定向访问

.htaccess 非www到www重定向访问,.htaccess,redirect,web,.htaccess,Redirect,Web,我有一个小问题,我不知道为什么 我试过: RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 及 但没有成功 它从以下位置重定向网站:http://domain.com 到http://www.domain.com 但不是来自http://domain.com/sample-page 到http://www.domain.com/sample-page 为什么 更新:我

我有一个小问题,我不知道为什么

我试过:

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

但没有成功

它从以下位置重定向网站:http://domain.com 到http://www.domain.com 但不是来自http://domain.com/sample-page 到http://www.domain.com/sample-page

为什么


更新:我使用HHVM3.6.6。这就是原因?!htaccess的位置在httpd-app.conf Bitnami HHVM中

如果希望在默认情况下应用此重定向,可以将其添加到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的更多信息

我希望有帮助


Jota

可能的重复可能是重复的,但这是针对提问者遗漏了一条尾随斜杠的情况。没有重复的重定向非www到www。现在此代码处于活动状态。重写cond%{HTTP_HOST}^www\。重写规则^.*${HTTP_HOST}/$1[R=301,L]
<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]
 ...
installdir/ctlscript.sh restart