kenklabeltraxx

Q: redirect all http to https

I am using OS X Server 5.0.15 with OS X 10.10.5.

 

I have 2 domains pointing to IP address of my OS X Server machine using DNS.

 

users.newsite.com and users.oldsite.com

 

The domain setup for the OS X Server machine is users.newwebsite.com

 

This is setup as the Server Website and I also have a Server Website (SSL) setup as well.

 

I have a redirect setup for "Server Website" to redirect to Server Website (SSL)

 

This part is working properly.

 

The problem is with the second domain.  If people type in users.oldwebsite.com they get an error message that the connection is not secure.  It appears the browser is trying to redirect http://users.oldsite.com to https://users.oldsite.com.

 

I need any calls to http://users.oldsite.com to redirect to https://users.newsite.com

 

Or really anything beginning with http://users. should redirect to https://users.newsite.com

 

Is this possible using regex?  If so, what would the expressions look like?

OS X Yosemite (10.10.5)

Posted on Jun 1, 2016 10:43 AM

Close

Q: redirect all http to https

  • All replies
  • Helpful answers

  • by MrHoffman,

    MrHoffman MrHoffman Jun 1, 2016 5:21 PM in response to kenklabeltraxx
    Level 6 (15,612 points)
    Mac OS X
    Jun 1, 2016 5:21 PM in response to kenklabeltraxx

    The Apache web site and documentation is the canonical source for information on mod_rewrite and RewriteRule examples, and mod_alias and Redirect examples.

     

    A slightly tweaked example from the Apache docs:

    <VirtualHost *:443>

      ServerName undesired.example.com

      ServerAlias example.com notthis.example.com

     

      Redirect "/" "https://www.example.com/"

    </VirtualHost>

     

    <VirtualHost *:443>

      ServerName www.example.com

    </VirtualHost>

     

    The second VirtualHost entry is the one for the host site you want — you'll almost certainly already have that block present in your configuration file:

    /library/server/web/config/apache2/httpd_server_app.conf

     

    There are various examples and discussions of this posted around the 'net as well, such as this one at StackOverflow.

     

    FWIW, newsite.com and oldsite.com are real and registered domains.

  • by John Lockwood,Helpful

    John Lockwood John Lockwood Jun 2, 2016 11:58 AM in response to kenklabeltraxx
    Level 6 (9,230 points)
    Servers Enterprise
    Jun 2, 2016 11:58 AM in response to kenklabeltraxx

    It sounds like you want to have -

     

    http://users.oldsite.com/ redirect to https://users.newsite.com/

    and

    http://users.newsite.com/ also redirect to https://users.newsite.com/

     

    This will mean only a single site will be accessible - the new one. If this is indeed what you want then I would have thought you need to first create three sites in the Mac web server.

     

    http://users.oldsite.com

    http://users.newsite.com

    and

    https://users.newsite.com

     

    You then need to define two redirects, one on each of http://users.oldsite.com and http://users.newsite.com each of which redirects to https://users.newsite.com/

     

    I define these redirects in the GUI of the Server.app the two http sites do not need any content at all since they never display anything as they immediately redirect to the single https site. The redirects would be source / and type 301 i.e. permanent. In theory it is not necessary to include the port number so https://users.newsite.com/ should work just as well as https://users.newsite.com:443/ however it would do no harm to include the port number.

  • by kenklabeltraxx,

    kenklabeltraxx kenklabeltraxx Jun 2, 2016 11:17 AM in response to John Lockwood
    Level 1 (4 points)
    Servers Enterprise
    Jun 2, 2016 11:17 AM in response to John Lockwood

    Hi John,

     

    Thank you, this seems to be working, at least partially.  If I enter http://users.oldsite.com or http://users.newsite.com they are redirecting to https://users.newsite.com.

     

    However, if I enter the URL for a specific page such as http://users.oldsite.com/projects.html I get the warning that my connection is not secure as it's trying to connect with https://users.oldsite.com/projects.html rather than redirecting to https://users.newsite.com/projects.html.

     

    I setup the redirect for http://users.oldsite.com via the GUI.  Source is / and Destination is https://users.newsite.com.

     

    The redirects are working properly for all URLs from http://users.newsite.com to https://users.newsite.com

     

    The redirect for http://users.newsite.com is set as Source: This Website and Destination is https://%{SERVER_NAME}

     

    If I try to enter this same redirect for http://users.oldsite.com it will not allow me to save the redirect.  The OK button is disabled.

     

    Any further help would be greatly appreciated.

  • by John Lockwood,Helpful

    John Lockwood John Lockwood Jun 13, 2016 11:11 AM in response to kenklabeltraxx
    Level 6 (9,230 points)
    Servers Enterprise
    Jun 13, 2016 11:11 AM in response to kenklabeltraxx

    The redirects of source / just do this for the bare host URL and not sub-urls. While it is I believe possible to define a redirect rule in a .htaccess file which is cleverer and able to do it for all urls for that site, if there is a limited number of sub-urls then just add additional rules with the sub-urls defined as the source.

     

    Something like the following would be what you put in the .htaccess file.

     

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

     

    Note: You need to enable i.e. turn on support for using .htaccess files in the website in Server.app

  • by kenklabeltraxx,

    kenklabeltraxx kenklabeltraxx Jun 13, 2016 11:16 AM in response to John Lockwood
    Level 1 (4 points)
    Servers Enterprise
    Jun 13, 2016 11:16 AM in response to John Lockwood

    Hi John,

     

    I apologize for the delay, I've just gotten back to working on this issue.  I've turned on allow overrides using .htaccess files.  Does the .htaccess file get created automatically?  I'm not sure where the file should be located.  I tried showing hidden files on the Mac and looking in the Sites folder, but I don't see a .htaccess file.  Any suggestions on where to look for or place the .htaccess file?

     

    Thanks in advance,

     

    Ken

  • by MrHoffman,

    MrHoffman MrHoffman Jun 13, 2016 3:44 PM in response to kenklabeltraxx
    Level 6 (15,612 points)
    Mac OS X
    Jun 13, 2016 3:44 PM in response to kenklabeltraxx

    .htaccess is manually created in and should be located in the top-level directory associated with the web site; at the top of the particular directory tree associated with the Apache web site.

  • by kenklabeltraxx,

    kenklabeltraxx kenklabeltraxx Jun 14, 2016 9:46 AM in response to MrHoffman
    Level 1 (4 points)
    Servers Enterprise
    Jun 14, 2016 9:46 AM in response to MrHoffman

    I have added the .htaccess file to the top-level directory for the site and turned on allow overrides using .htaccess files, but now the http and https sites for  "users.newsite.com" say available on the local network, not via the internet, and the http for "users.oldsite.com" does not have a green dot next to it in the websites list.  Something is wrong, maybe I don't have the correct permissions for the .htaccess file?  I'm not sure which users or groups need access and what type of access they need?

  • by John Lockwood,

    John Lockwood John Lockwood Jun 14, 2016 10:19 AM in response to kenklabeltraxx
    Level 6 (9,230 points)
    Servers Enterprise
    Jun 14, 2016 10:19 AM in response to kenklabeltraxx

    The file needs to be owned by the _www account I believe just like all the other web pages. However more importantly you are going to need at least two copies of this file one for users.newsite.com and one for users.oldsite.com.

     

    (Correction, it seems from looking at one of my servers that it defaults to using the root account to 'own' the contents of a website folder although from a Linux perspective this is considered to be bad security practice.)

     

    My example code merely redirects all http traffic to https and will do this for all URLs for a site including subpages. The example I gave will not redirect from users.oldsite.com to users.newsite.com.

     

    Either you will have to add additional rules in your .htaccess which both redirects to the users.newsite.com or you will need to try and combine the use of a .htaccess file with the Server.app defined redirect rules.

     

    My example .htaccess was only intended as a suggestion not the complete solution. I am not a web developer myself so I am not an expert on all the permutations needed.

     

    A guess for a combined rule might be as follows -

     

    RewriteRule (.*) https://users.newsite.com%{REQUEST_URI}

  • by kenklabeltraxx,

    kenklabeltraxx kenklabeltraxx Jun 14, 2016 10:40 AM in response to John Lockwood
    Level 1 (4 points)
    Servers Enterprise
    Jun 14, 2016 10:40 AM in response to John Lockwood

    Thanks for the response.  I'm obviously not a web developer either.  I'm an IT guy for a small company just trying to get this to work.  I've never messed with .htaccess so this is all new.  Thanks again.

  • by MrHoffman,

    MrHoffman MrHoffman Jun 14, 2016 2:35 PM in response to kenklabeltraxx
    Level 6 (15,612 points)
    Mac OS X
    Jun 14, 2016 2:35 PM in response to kenklabeltraxx

    kenklabeltraxx wrote:

     

    Thanks for the response.  I'm obviously not a web developer either.  I'm an IT guy for a small company just trying to get this to work.  I've never messed with .htaccess so this is all new.  Thanks again.

     

    Check the Apache docs linked earlier.   The Apache docs are good, detailed, and — once you have the keywords as some search strings — there are more than a few examples of common sequences posted both at the Apache site and around the net, and — if you prefer it — there are also books available.   (There's very little specific to MacOS when working with Apache, beyond the paths used and a few other and minor details.  The vast majority of the details — including the rewrite rules and related syntax – are common across all platforms, MacOS or otherwise.)