mod_rewrite的 - 增加WWWmod_rewrite、WWW

2023-09-02 00:44:09 作者:ゆ镜中孤独少一人

我试图迫使 WWW 我的网站地址与的.htaccess

I'm trying to force www for my site address with .htaccess:

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

如果我打开 mysite.com 这个工作确定 - 它增加了 WWW 。它成为 http://www.mysite.com/index.php

If I open mysite.com this working ok - it adds www. It becomes http://www.mysite.com/index.php.

但是,如果我打开 mysite.com/subpage 我重定向到 http://www.mysite.com/index.php 再次,而不是 http://www.mysite.com/subpage

But if I open mysite.com/subpage I redirected to http://www.mysite.com/index.php again, instead of http://www.mysite.com/subpage.

但是,如果我打开 http://www.mysite.com/subpage 我没有得到重定向(这是预期的行为)。

But if I open http://www.mysite.com/subpage I don't get redirect (which is the expected behaviour).

我该如何解决这个问题?我想重定向所有请求到我的网站的 WWW 子域。

How can I fix this? I would like the redirect all requests to my site to the www subdomain.

推荐答案

的问题就在这里:

RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]

^ 指定字符串的开头,而 $ 指定结束。

^ specifies the beginning of the string while $ specifies the end.

为了使它工作,你需要有.COM之后通配符选择或 +

In order to make it work you need to have a wildcard select after the .com or .+.

要重申亚历克斯说:

 <IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} !=on
   RewriteCond %{HTTP_HOST} !^www..+$ [NC]
   RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>