0

I'm using the following mod rewrites to ensure not only canonical URLs, but also that the site is displayed using HTTPS:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
// It think the problem must be here --^

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php?
RewriteRule ^index\.php?$ https://www.rto12.ca/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?
RewriteRule ^index\.html?$ https://www.rto12.ca/ [R=301,L]

My problem comes when you try to go here: rto12.ca... The browser takes you here: `https://www.rto12.ca/https://rto12.ca/'

It's the first condition/rule that's causing this. Any suggestions would be appreciated.

2
  • What are you using to force the HTTPS? I don't see a rule that checks for that here.
    – Tim Stone
    Commented Sep 7, 2010 at 14:59
  • Oh good point Tim, and probably where my issue lies: RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    – jay
    Commented Sep 7, 2010 at 15:05

1 Answer 1

4

This rule:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

...will just rewrite the request to https://rto12.ca/REQUEST_URI, and then pass it off to the next rule (the input to the next rule, which you append to the end of the request, will be https://rto12.ca/REQUEST_URI). However, for it to work properly, you need it to redirect immediately:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It's likely possible to combine all of your rules into at most a single redirect, so let me play around with it a bit and I'll see what I can come up with, then I'll update the answer. Adding the flags should fix your problem either way, though.

Edit: I think this should take everything in one go:

RewriteEngine On

RewriteCond %{HTTPS}        =off   [OR]
RewriteCond %{HTTP_HOST}   !^www\. [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]{3,9}\ /index\.(html|php)
RewriteCond %{HTTP_HOST}    ^(www\.)?(.+)$
RewriteRule ^(index\.(html|php))|(.*)$ https://www.%2/$3 [R=301,L]
3
  • This definitely did it Tim, thanks! If you come up with a single redirect to do what I'm doing here I'd love to see it. Thanks again.
    – jay
    Commented Sep 7, 2010 at 15:21
  • @jeerose - Cool, glad it's working. I've updated the answer with a combined redirect block, see if that takes care of everything you wanted.
    – Tim Stone
    Commented Sep 7, 2010 at 15:28
  • It does indeed. Thank-you so much for your time, Tim.
    – jay
    Commented Sep 7, 2010 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.