user3452136
user3452136

Reputation: 125

How do I set canonical URLs and redirect to https?

I have redirects working for http to https, as well as canonicalization for non www URLs to redirect to WWW version…well, sort of.

http://www.domain.com redirects to https://www.domain.com

http://domain.com redirects to https://www.domain.com

https://www.domain.com loads fine

However, https://domain.com won't connect (different browsers give different messages).

I verified that mod_rewrite is enabled. Here's the code below:

#Redirect to SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}[R01,L]
#Canonicalization
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1[L,R01]

Two questions:

1.) Why doesn't https://domain.com route to https://www.domain.com?

2.) I've seen the flag R=301 for redirects, but what does R01 mean?

Thanks!

Upvotes: 0

Views: 559

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You can combine the two like this:

RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

A few things:

  1. R01 is incorrect, it's not a flag, and would have caused a 500 error if it wasn't for a missing space before your flags
  2. You're missing a space that separates your target URL and the flags: $1[L,R01] ends up being interpreted as part of the target URL.

Upvotes: 1

Related Questions