0

How can I redirect my website from all HTTP requests to HTTPS and non-www HTTPS to HTTPS with www?

Example:

Redirect HTTP non-www, HTTP www. & HTTPS non-www:

HTTP://example.com/ && HTTP://www.example.com/ && HTTPS://example.com/

everything to HTTPS with www.:

HTTPS://www.example.com/

7
  • Do you have any redirects in place currently? Is it just the one domain, or are there multiple names to deal with? May 1 '14 at 22:55
  • @ShaneMadden One domain. There are few redirects already in place like: RewriteRule ^search search.php [L] RewriteRule ^games/(.*).html?$ games.php?do=$1 [L]
    – SubZeroFX
    May 1 '14 at 22:59
  • What does your current virtual host config look like - one HTTP and one HTTPS, or different ones for www and non-www? (apachectl -S) May 1 '14 at 23:12
  • I would like to do that redirects in single .htaccess file
    – SubZeroFX
    May 1 '14 at 23:18
  • Are you sure that's how you want to do this? See here. May 1 '14 at 23:19
5
# Redirect all "not correct" domain to www with https
# (is not important if comes with or without https):
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# Redirect all non-ssl to ssl.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

I think this will work with your actual rules.

1
  • The last rule should ultimately be a 301 (permanent) redirect. So, R should be changed to R=301 (otherwise it defaults to a 302 - temporary - redirect). The NC flag on the first condition should also be removed, since this is a negated condition. Otherwise, it won't catch mix cased (malformed) Host headers.
    – MrWhite
    Sep 3 '20 at 11:30
0

This will use for both www or non-www If you try to open link with www then url redirect to https with www

Example : http://domain.com redirect to https://domain.com

or If you try to open link with non-www then url redirect to https with non-www

Example : http://www.domain.com redirect to https://www.domain.com

RewriteEngine on

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
1
  • This is only an HTTP to HTTPS redirect (same host). It does not canonicalise the www subdomain as requested in the question.
    – MrWhite
    Sep 3 '20 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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