Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

how to modify .htaccess file to always redirect to www

I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.

Here is my current htaccess file:

<IfModule mod_rewrite.c>
   RewriteBase /
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]  
</IfModule>

Normally I know how to do the redirect, but im having issues since it already has those few lines in there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.

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

(edited to have all the code in the same block)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...