0
votes

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
I guess you’ll have to do this separately for each case, because there isn’t really a way to get this info as a back reference or something like that AFAIK. An additional RewriteCond that checks for HTTPS, preceding a Rule that rewrites to https://... explicitly. - CBroe
Thanks @CBroe. Any idea how to do this? I'd rather not do them individually as there are about 100 https ppc landing pages - and over 400 http SEO pages. - Josh N
By each case I meant HTTP and HTTPS, not each individual URL ... - CBroe
Ok. How? I have rather limited knowledge of .htaccess - Josh N

2 Answers

1
votes

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>
0
votes

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.