.htaccess redirect www to non-www not working

时光怂恿深爱的人放手 提交于 2020-01-06 02:23:14

问题


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:

redirect www to non-www

However, what I get is that if I enter www.example.com, I get:

example.com/example.com/ and a 404 error on my site

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?


回答1:


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

That is, check if the HTTP_HOST contains www and if so, redirect to non-www with the request_uri appended.



来源:https://stackoverflow.com/questions/32942701/htaccess-redirect-www-to-non-www-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!