.htaccess将www重定向到非www不起作用 [英] .htaccess redirect www to non-www not working
问题描述
关于.htaccess编辑或正则表达式,我几乎一无所知.为了使我的网站从 www
版本重定向到 non-www
版本,我所做的就是遵循以下发布的答案:
I pretty much know nothing about about .htaccess editing or regex expressions. What I have done to try to make my site re-direct from www
versions to non-www
versions is I followed the answers posted here:
但是,我得到的是,如果输入 www.example.com
,我会得到:
However, what I get is that if I enter www.example.com
, I get:
example.com/example.com/
和我的网站上出现404错误
example.com/example.com/
and a 404 error on my site
这是我的.htaccess文件:
This is my .htaccess file:
#Adjust default time zone
SetEnv TZ America/Toronto
#Begin redirect www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
# Google Analytics Integration - Added by cPanel.
<IfModule mod_substitute.c>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<script src='/google_analytics_auto.js'></script>)?</head>|<script src='/google_analytics_auto.js'></script></head>|i"
</IfModule>
# END Google Analytics Integration
有人可以启发我做错什么吗?
Can someone enlighten me on what I am doing wrong?
推荐答案
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
也就是说,检查HTTP_HOST是否包含www,如果包含,则重定向到附加了request_uri的非www.
That is, check if the HTTP_HOST contains www and if so, redirect to non-www with the request_uri appended.
这篇关于.htaccess将www重定向到非www不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!