如何重定向非www到www网址的使用htaccess的?

问题描述:

我有一个网站说href="http://www.example.com/"> http://www.example.com/ 在我的网站的根目录下,我加入了 http://example.com/ 的任何请求重定向到的 http://www.example.com/

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/

最近我创建了一个新的部分视频这样的URL,视频是 http://www.example.com/videos/ 。在这个视频的文件夹我有一个正在执行改写为参赛视频另htaccess文件。当我试图访问 http://example.com/videos/ 那么它不重定向我的http://www.example.com/videos/

Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/

我觉得.htacces是不是继承了父目录previous规则。谁能告诉我有什么方法可以治我可以添加在/视频/文件夹中的.htaccess文件,以便为 HTTP的任何请求://例子.COM /视频/ 将被重定向到 http://www.example.com/videos/ 的URL。

I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.

这是一个更通用的解决方案,因为它可以与任何域名使用,而无需在每个的.htaccess指定特定的域名:

This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:

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

相反的也有可能(WWW到非www):

The contrary is also possible (www to non-www):

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]