Ad

.htcaccess File Assistance Needed For Canonicalization

- 1 answer

My website currently has two version to google, https://www.example.com and a https://example.com This is apparently not good for SEO as its leaking all my seo 'juice' away. I have already told google to use just the www version around 5 weeks ago but come results on google still show the non-www version. I was wondering if anyone could help with my .htaccess to get the redirect right to just www.

The htcaccess file is copied below:

RewriteEngine on
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(blog)

RewriteCond %{HTTP_HOST} ^`.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.`.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteRule ^(.+)$ /index.php [L]
Ad

Answer

For a simple redirect you can use:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want to also force HTTPS:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Note: This block goes above the prettify?-->

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Note: This block goes above the www. rewrite rules to make sure both rules take hold

Ad
source: stackoverflow.com
Ad