skip to Main Content

I have a client who has an http SEO’ed wordpress site, with https AdWords static html landing pages (I know – don’t ask).
They want to 301 all non-www to www. I tried the following htaccess code:

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

This works great for getting all URLs redirected to www from non-www. However it also changes all https URLs to http.

How can I get it so that all non-www redirect to www, but http and https are NOT affected.

For example:

http://example.com/seo-page -> http://www.example.com/seo-page

and

https://example.com/ppc-page -> https://www.example.com/ppc-page

Thanks

EDT:
Existing .htaccess is:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* - [E=CANONICAL:http://%{HTTP_HOST}%{REQUEST_URI},NE]
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=CANONICAL:https://%{HTTP_HOST}%{REQUEST_URI},NE]
</IfModule>

2

Answers


  1. Try this :

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^(www.)
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTPS} =on
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    Save .htaccess and try this :

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    RewriteCond %{HTTP_HOST} !^(www.) 
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTPS}:s on:(s)
    RewriteRule .* - [E=CANONICAL:http%1://www.%{HTTP_HOST}%{REQUEST_URI},NE]
    
    RewriteCond %{HTTP_HOST} ^(www.)
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTPS}:s on:(s)
    RewriteRule .* - [E=CANONICAL:http%1://%{HTTP_HOST}%{REQUEST_URI},NE]
    </IfModule>
    
    Login or Signup to reply.
  2. If you are using WordPress then you don’t need to add any code in .htaccess file. You can redirect entire site by entering www or nonwww url in settings.

    Login to wp-admin
    Then hit settings button and visit in
    the 3rd field name (WordPress Address (URL)) enter your URL where you want to redirect website. and paste the same URL in next field name (Site Address (URL))

    and then Hit Save button.

    Congratulation your website redirected successfully.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search