Search code examples
.htaccessseohttp-redirectno-www

How to redirect non-www to www using htaccess?


I'm trying to do a htaccess redirect from the non-WWW url to the WWW url.

I saw alot of example in this forum like: Redirect non-www to www in .htaccess but none of them work like I want.

My url is: www.evo.co.il and a page in my site for example is www.evo.co.il/בניית-אתרים getting a slug in url and redirect to the page (by htaccess)

so I want that it will work like this:

evo.co.il -> www.evo.co.il

evo.co.il/בניית-אתרים -> www.evo.co.il/בניית-אתרים

some of the example I saw just working for the root domain and not for the pages, some other's redirect the inner pages in this way:

evo.co.il/בניית-אתרים -> www.evo.co.il/page.php?id=1

I tried those code (and many other versions):

1.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^evo.co.il$
RewriteRule (.*) http://www.evo.co.il/$1 [R=301,L] 
RewriteRule ^בניית-אתרים$ /page.php?id=1 [L]

2.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^evo.co.il
RewriteRule (.*) http://www.evo.co.il/$1 [R=301,L] 
RewriteRule ^בניית-אתרים$ /page.php?id=1 [L]

3.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^evo.co.il$ [NC]
RewriteRule ^(.*)$ http://www.evo.co.il/$1 [L,R=301]
RewriteRule ^בניית-אתרים$ /page.php?id=1 [L]

What is the right way to do it?


Solution

  • Why not simply:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !www\.evo\.co\.il$ [NC]
    RewriteRule ^(.*)$ http://www.evo.co.il/$1 [L,R=301]
    
    RewriteRule ^בניית-אתרים$ /page.php?id=1 [L]