1

I have a site hosted on godaddy. uses apache. I used this code in .htaccess to add www prefix to the domain automatically

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

but instead of 'www.example.com' it goes to 'www.example.com/web'

I just want to convert 'example.com' to 'www.example.com'

Cœur
  • 37,241
  • 25
  • 195
  • 267
imeshU
  • 25
  • 4
  • Possible duplicate of [.htaccess redirect - automatically add www. if no subdomain exists](http://stackoverflow.com/questions/12256130/htaccess-redirect-automatically-add-www-if-no-subdomain-exists) – Ron van der Heijden Mar 24 '17 at 11:28

2 Answers2

1

If you just want to convert example.com to www.example.com then you just need to use:

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

You can also lay it out like this:

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

Make sure you clear your cache before testing this. You will notice I've just the flag R=302. This is a temporary redirect, use this while you're testing. If you're happy with the RewriteRule and everything is working, change these to R=301, which is a permanent redirect.

Joe
  • 4,877
  • 5
  • 30
  • 51
0

solved by using this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
RedirectMatch 301 ^/web/$ http://www.example.com/
imeshU
  • 25
  • 4