imeshU
imeshU

Reputation: 25

using .htaccess to add www prefix

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'

Upvotes: 1

Views: 475

Answers (2)

imeshU
imeshU

Reputation: 25

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/

Upvotes: 0

Joe
Joe

Reputation: 4897

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.

Upvotes: 1

Related Questions