0

I am trying to change my url when someone visits my site so non-www redirects to www ...http://example.com to http://www.example.com

I have found this code

RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]

But when i add it to my .htaccess i get a error....is this the wrong code ?? How do i add this to my .htaccess with out getting a error?

My .htaccess

RewriteEngine On
RewriteCond    %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ / profile.php?username=$1
Ejaz
  • 8,719
  • 3
  • 34
  • 49
Stuart Compaan
  • 177
  • 1
  • 1
  • 10

3 Answers3

1

Add following lines to your .htaccess file;

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

Source:

Community
  • 1
  • 1
Turcia
  • 653
  • 1
  • 12
  • 29
1

You can add that, just below RewriteEngine On :

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Croises
  • 18,570
  • 4
  • 30
  • 47
0

Overrite your .htacess file with following code.

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

RewriteCond    %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ / profile.php?username=$1

More understanding of .htaccess rules:
http://www.web-technology-experts-notes.in/2014/02/htaccess-code-snippets-example.html

Arun Kumar
  • 36
  • 4