0

I'm looking for a way to redirect users to https://domain.com whether they type the www or not

I'm using a shared host for a drupal website so i don't have access to virtual host config files. A let's encrypt certificate if included with the hosting package
Right now the website can be accessed both with http and https depending on what you type in the browser.
In looking for a solution to force redirection to https i found this section in the in the .htaccess file that redirects users who type the www. to https://domain.com

# To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment the following:
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ https%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

Now i'm looking for a way to redirect when typing http://domain.com without the www.

Do i need to put another rule somewhere in addition to the line i uncommented or is there some other way to do this ?

2 Answers 2

1

The official stanza in .htaccess to redirect all traffic to HTTPS, recommended by Acquia is this:

# Redirect to HTTPS.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
0

I'm using this piece of code for both situations (with and without www.):

# Redirect to HTTPS and remove `www.` from domain.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.DOMAINNAME\.com*
RewriteRule ^(.*)$ https://DOMAINNAME.com/$1 [L,R=301]

And it works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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