Home > database >  .htaccess redirect entire site from non-www/http to www/https
.htaccess redirect entire site from non-www/http to www/https

Time:11-16

I know this has been asked a lot on here but I still can't find an answer that works for my website. These are the posts/answers I've tried:

I am getting duplicate content errors showing up, here is an example

https://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/
http://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/

These URLs are showing up as duplicate content so I am trying to force a 301 redirect to

https://www.example.co.uk/coffee-machines/

and not just on that url but site wide. I have a "Canonical" tag set up that points to the correct version but I'd also like to 301 redirect all web pages/directories to the www/https version of the page.

This is my current contents of .htaccess

Options -Indexes

Options  FollowSymLinks

Options -MultiViews

AddDefaultCharset UTF-8

ErrorDocument 401 /401.php
ErrorDocument 404 /404.php

RewriteEngine on
RewriteBase /

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

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

and this works for redirecting example.co.uk to the www/https version but no sub-pages.

Could someone please point me in the right direction to either redirect all urls to the www/https version of suggest a solution to stop the duplicate content.

I've changed the .htaccess to the code below to reflect the post being flagged as duplicate. So the .htaccess now reflects the answer in this post (Redirect non-www and non-http to https) and also included the entire .htaccess file contents.

I am checking the headers by starting a session with putty and then using (curl --head http://example.co.uk) as suggested by @StephenOstermiller

Options -Indexes

Options  FollowSymLinks

Options -MultiViews

AddDefaultCharset UTF-8

ErrorDocument 401 /401.php
ErrorDocument 404 /404.php

RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.xxx.co.uk%{REQUEST_URI} [NC,L,R=301,NE]

RewriteRule ^q&a/$ forum.php

RewriteRule ^q&a/(.*).html$ forumDetail.php?question=$1&%{QUERY_STRING} [B,L]

#RewriteRule ^q&a/(.*)$ /forum/$1 [R=301,NC,L]

RewriteRule ^blog/$ blog.php

RewriteRule ^blog/(.*).html$ blogDetail.php?blog_name=$1&%{QUERY_STRING} [B,L]

RewriteRule ^about-us/$ about.php
RewriteRule ^privacy-policy/$ privacy.php
RewriteRule ^disclaimer/$ disclaimer.php
RewriteRule ^my-account/$ user_account.php
RewriteRule ^activate-my-account/$ user_activate.php
RewriteRule ^update-my-account/$ user_change.php
RewriteRule ^login/$ user_login.php
RewriteRule ^logout/$ user_logout.php
RewriteRule ^register/$ user_register.php
RewriteRule ^reset-my-password/$ user_reset.php
RewriteRule ^home-energy/$ home-energy.php

RewriteRule ^compare/(.*)$ /kitchen-scales/$1 [R=301,NC,L]

RewriteRule ^kitchen_scales/(.*)$ /kitchen-scales/$1 [R=301,NC,L]
RewriteRule ^coffee_machines/(.*)$ /coffee-machines/$1 [R=301,NC,L]
RewriteRule ^digital_radios/(.*)$ /digital-radios/$1 [R=301,NC,L]
RewriteRule ^mixers_blenders/(.*)$ /mixers-blenders/$1 [R=301,NC,L]

RewriteRule ^4g-broadband-all-you-need-to-know.html$ 4g-broadband-all-you-need-to-know.php

Redirect 301 /kitchen-scales/category/digital-scales/index.php https://www.xxx.co.uk/kitchen-scales/category/digital-scales/
Redirect 301 /kitchen-scales/salter-kitchen-scales.html https://www.xxx.co.uk/kitchen-scales/brand/salter/
Redirect 301 /kitchen-scales/brand/index.php https://www.xxx.co.uk/kitchen-scales/brand/
Redirect 301 /kitchen-scales/brand/salter/index.php https://www.xxx.co.uk/kitchen-scales/brand/salter/

Redirect 301 /coffee-machines/category/bean-to-cup-coffee-machines/index.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.html https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/

<IfModule mod_headers.c>
  Header set Connection keep-alive
</IfModule>

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|webp|gif|html|htm|xml|txt|xsl|css)$">
Header set Cache-Control "max-age=31536000"
</FilesMatch>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

<FilesMatch "config\.php|config\.advanced\.php">
  Order allow,deny
  Deny from all
</FilesMatch>

<Files ~ "^.*\.([Hh][Tt][Aa])">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

AddType x-httpd-php73 .php

I'm also using LetsEncypt - not sure if that's relevant?

CodePudding user response:

When the problem is per-directory .htaccess files, you can prevent the rules in those files from running after some rules by using the [END] flag. From the documentation:

Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.

So you should be able to use [END] in your rules to make them take precedence over other rules in the subdirectories:

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

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,END]
  • Related