0

I have a website running on a dedicated server (single IP) on apache. Multiple domains are pointed to the website . For ex: sub.abc.com, sub.bcdaf.com

I have now purchased a single SSL certificate for one of the domains (sub.abc.com). Only SSL only works when user accesses https://sub.abc.com

I want to force users to https when they access http. And by default, when they access sub.abc.com, it should go to https://sub.abc.com . How can i do this via htaccess?

Please note that the https only works for sub.abc.com and should forcing users should happen only for sub.abc.com

1
  • 1
    This was cross-posted on serverfault.com/questions/543991/…. David, please refrain from cross-posting, it is not appreciated on this network. Also this question has little to do with security... you should learn to read documentation.
    – Lekensteyn
    Commented Oct 5, 2013 at 10:45

2 Answers 2

1

redirect as @Terry mentioned is ok, but will not work if users use bookmarks and you want to make them available also in HTTPS:

  • dont forget to use HSTS-Headers

  • Solution using .htaccess only (untested) (see apache-wiki)

    you can skip the %{HTTP_HOST} - condition if you use a
    <virtualhost> - config for each virtual host 
    and no catchall/regex-servernames
    
    --- first solution 
    RewriteCond %{HTTP_HOST} sub\.abc\.com
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/(.*)$ https://sub.abc.com/$1 [r=301,L]
    
    --- 2nd solution 
    RewriteCond %{HTTP_HOST} sub\.abc\.com
    RewriteCond %{SERVER_PORT} ^443
    RewriteRule ^/(.*)$ https://sub.abc.com/$1 [r=301,L]
    
  • Solution using Rewrite-Rule and with working deep links / server-config (prefered by official docs):

    <virtualhost :80>
        ServerName sub.abcd.com
        RewriteRule ^/(.*) https://sub.abcd.com/$1     [R=301,L]
    
    </virtualhost>
    
    <virtualhost :443>
         ServerName sub.abcd.com
         Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    </virtualhost>
    
1
  • 1
    Good point on the bookmarks issue. I forgot about the RewriteCond and RewriteRule directives.
    – user10211
    Commented Oct 5, 2013 at 12:18
0

You can accomplish this with the Redirect directive. This works using both .htaccess and in VirtualHost configurations in httpd.conf.

Redirect permanent / https://sub.abc.com
5
  • Does this work when users access inner pages directly like sub.abc.com/user/add ?
    – david
    Commented Oct 5, 2013 at 5:14
  • @david I don't have apache handy to test it right now but it should. You can also use a directive such as Redirect permanent /user/add https://sub.abc.com/user/add if you only want a redirect in certain pages.
    – user10211
    Commented Oct 5, 2013 at 5:16
  • @david I think I made a little mistake so I edited my answer. I'm going from memory though, so you will have to play around with the Redirect directive a little.
    – user10211
    Commented Oct 5, 2013 at 5:17
  • I only want this work when users access any page of sub.abc.com and not any other domain
    – david
    Commented Oct 5, 2013 at 5:18
  • But the solution above will do a redirect for all domains. Thank you.
    – david
    Commented Oct 5, 2013 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .