mod_rewrite的 - 增加WWW [英] mod_rewrite - add www

查看:88
本文介绍了mod_rewrite的 - 增加WWW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图迫使 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>

这篇关于mod_rewrite的 - 增加WWW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆