Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
Randy Avatar asked Nov 11 '10 20:11

Randy


People also ask

How do I redirect to www?

Redirecting Your Domain to www URL. Keep the Type as the default, Permanent(301). Select your domain name from the drop down menu on the next line. In the redirects to text box, type in the full URL of your domain, including www (e.g. http://www.yourdomain.com).

How do I redirect all non-www to www?

The easiest way of redirecting a non-www URL to www is to place a rule in the . htaccess file. You can do so via FTP, SSH, or your hosting account's control panel. hPanel users can easily access and edit the .


2 Answers

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)

like image 172
Teacha Kyng Avatar answered Sep 20 '22 06:09

Teacha Kyng


Add something like this immediately after RewriteEngine on:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
like image 44
Laurence Gonsalves Avatar answered Sep 19 '22 06:09

Laurence Gonsalves