WebFaction
Community site: login faq

Apologies in advance. I know this is a old question that keeps getting asked but I haven’t found a solution that works correctly for my needs.

I need to direct ALL traffic to reach the non-WWW version of HTTPS website, and I need it to work on multiple domains, not just a single one.

example:

http//example.com -> https//example.com http//www.example.com -> https//example.com https//www.example.com -> https//example.com

http//example2.com -> https//example2.com http//www.example2.com -> https//example2.com https//www.example2.com -> https//example2.com

example3, 4 ,5, etc.

==

Until now I haven’t been using HTTPS, just HTTP. I’ve been redirecting all HTTP requests from non-WWW to WWW.

I created an app specifically to redirect: - all non-WWW URLs were added to the redirection_app - all WWW URLs were added to their respective website_app

I placed the following rule inside the .htaccess file for the redirection_app:

RewriteEngine on RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]

This works fine, but now I want to drop the www and ensure the website is only served over HTTPS: www -> non-www http -> https

*.example.com -> https//example.com

asked 26 Oct, 01:35

wicky's gravatar image

wicky
314
accept rate: 0%


Hi there!

Much like with the WWW-redirects, you'll need one Application to map all your HTTP domains to, which will contain several redirect rules. The .htaccess of the redirect Application will contain two condition-based RewriteRules - one to handle requests from www-domains and one for non-www domains (http://example.com -> https://example.com AND http://www.example.com -> https://example.com):

RewriteEngine On

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

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

Note that if you would also like to redirect HTTPS requests from www-domains to their non-www versions (https://www.example.com -> https://example.com), you will need another redirection Application to handle all HTTPS requests to www-domains. It will have a different set of rules in its .htaccess file:

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

You would only map the www-versions of the domains to this HTTPS redirect Application, again if you wish to redirect HTTPS requests of www-domains to their non-www counterparts, which will of course be mapped to the actual Applications serving the appropriate content.

Finally, I would like to say that some CMS systems such as Wordpress, with which you can set the preferred URL parameter for a specific website will not require the additional redirect Application, since they provide permalink redirects of their own through PHP. Namely, if you have a Wordpress instance with its home and siteurl parameters set to the non-www version of the domain, Wordpress will perform the redirect after the HTTPS redirect takes place.

permanent link

answered 26 Oct, 08:21

hristod's gravatar image

hristod ♦♦
261
accept rate: 100%

Thank you for the help @hristod, it’s much appreciated! The first part works perfectly. Both HTTP requests (www AND non-www) redirect to HTTPS non-www. However the 2nd redirection app for HTTPS gives an insecure website notice when I test https://www.example.com. I have valid certificates for each site (both www and non-www), but I can’t apply multiple certificates to a generic app that handles multiple domains. Is there a way around this? I know I could apply the 2nd .htaccess content to each individual application’s .htaccess , but the HTTPS redirection app is much more preferable.

(26 Oct, 18:38) wicky wicky's gravatar image

@wicky no, there is no way around that. If you need to use multilple certificates, then you'll have to use multiple sites.

(26 Oct, 18:45) seanf ♦♦ seanf's gravatar image

Ok. Thanks for your help @seanf and @hristod.

(27 Oct, 22:41) wicky wicky's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×72
×70

question asked: 26 Oct, 01:35

question was seen: 198 times

last updated: 27 Oct, 22:41

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2017 PARAGON INTERNET GROUP LIMITED
WEBFACTION IS A SERVICE OF PARAGON INTERNET GROUP LIMITED
REGISTERED IN ENGLAND AND WALES 7573953 - VAT REGISTRATION NUMBER 182147021
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM