2

I'm trying to manage the canonicals for my website using htaccess.

I recently switched to HTTPS and now my old rules aren't working:

#URLS ALWAYS CHANGED TO WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

#PREVENT TRAILING '/' AT END OF URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# PREVENT CANONICALIZATION DUPLICATES
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

There's a bit of a catch though. If a connection is through HTTP I want it to stay as HTTP.

I don't want to force HTTPS, as I need HTTP sometimes for visitor security reasons - so embedded content can't piggyback on my https authority:

Ordinary people don't understand what an IFRAME is. They will just look at the padlock and https at the topbar and think the site is safe to put their credit card details - when actually they may be accessing an untrusted HTTP site (or worse). Therefore pages that display uncontrolled IFRAME content I always display as http.

The pages 301 redirect if the wrong url is being used anyway. But I'd rather do this with htaccess as well for more redundancy and a faster response.

So this is basically what I need to do:

Amy Neville
  • 10,067
  • 13
  • 58
  • 94
  • 1
    _“I don't want to force HTTPS, as I need HTTP sometimes for visitor security reasons (so embedded content can't piggyback on my https authority)”_ – I don’t understand how that is supposed to increase security, resp. what you mean by embedded content piggy-backing. – CBroe Jan 26 '16 at 10:25
  • 1
    Anyway, if you want _conditions_ to your HTTP to HTTPS redirect – then you need to apply those conditions before you rewrite non-www to `https://www.` – CBroe Jan 26 '16 at 10:26
  • @CBroe card security for people visiting the site. People look at the topbar and it will say HTTPS with a padlock and they might think it is safe to type in card details. But they will actually be looking at an IFRAME of another site that might be http or worse. Ordinary people don't understand this sort of thing - they just look at the topbar for the padlock. – Amy Neville Jan 26 '16 at 10:36
  • 1
    What about: http://stackoverflow.com/questions/19372594/preserve-http-https-protocol-in-htaccess-redirects – Brandon Harris Jan 27 '16 at 06:19

0 Answers0