How to change .htaccess to redirect all non-www links to www pages?

我们两清 提交于 2019-12-11 06:13:48

问题


How can I redirect all non-www links to www links? I have found solutions on the internet, but they only redirect the domain name. How do I make this general: http://example.com/testing should redirect to http://www.example.com/testing?


回答1:


try something like this

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



回答2:


RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.yourdomain.com/$1

If you want something generic that works for any domain, you can try something like:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule ^(.*)$ http://www.%1/$1


来源:https://stackoverflow.com/questions/1659949/how-to-change-htaccess-to-redirect-all-non-www-links-to-www-pages

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