简体   繁体   English

如何使用htaccess将非www重定向到www URL?

[英]How to redirect non-www to www URL's using htaccess?

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/ 我的网站根目录中有一个说http://www.example.com/的网站,我添加了.htaccess文件以将http://example.com/的任何请求重定向到http://www.example。 com /

Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . 最近,我创建了一个新部分“视频”,因此视频的URL为http://www.example.com/videos/ In this video folder I have another htaccess file which is performing rewriting for video entries. 在此视频文件夹中,我还有另一个htaccess文件,该文件正在执行视频条目的重写。 When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/ 当我尝试访问http://example.com/videos/时,它无法将我重定向到http://www.example.com/videos/

I think .htacces is not inheriting the previous rules from the parent directory. 我认为.htacces不会从父目录继承以前的规则。 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. 谁能告诉我我可以在/ videos /文件夹的.htaccess文件中添加的规则是什么,以便对http://example.com/videos/的任何请求都将重定向到http://www.example。 com / videos / URL。

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: 这是一个更通用的解决方案,因为它可以与任何域名一起使用,而不必在每个.htaccess中指定特定的域名:

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

The contrary is also possible (www to non-www): 相反也可能(从www到非www):

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

I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. 我认为这可能只是您现有的规则过于严格,因此不会在您的子目录中触发它。 Something like this should work site-wide: 这样的事情应该在整个网站范围内起作用:

RewriteEngine on
RewriteBase /

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

This is Best Solutions to redirect non-www to www URL's using htaccess. 这是使用htaccess将非www重定向到www URL的最佳解决方案。 using this code in htaccess file and check url. 在htaccess文件中使用此代码并检查url。

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host. 我建议大家坚持使用以下方法,它将仅重定向主域,而不重定向主机中的任何其他子目录或子域。

# Redirect non-www to www only for main domain 
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /

# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]

Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website ie subdomain.yoursite.com or whatever. 如果您确实有把握,请使用此方法,希望您永远不要在您的网站上使用子域,例如subdomain.yoursite.com或其他任何形式。 Than you're welcome to use the below method. 欢迎使用以下方法。 "Saying Again make sure if you really want to use this method" “再说一次,请确保您是否真的要使用此方法”

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

The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500. 尽管您不能使用www.subdirecty.yoursite.com,以下方法会将所有www网址重定向到非www,包括您的子域,但它会提示您输入500错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM