Linux Redirecting HTTP URLs to HTTPS

Discussion in 'Software' started by OneSeventeen, 10 Jan 2005.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    I am going to be installing an SSL Certificate (as soon as it gets done with purchasing and authenticating, which around here should take a week or two) and I would like to force everyone to use https://foo.example.com instead of http://foo.example.com

    this is a single-purpose application, so forcing people from http://foo.example.com/path/to/file to https://foo.example.com is acceptable.

    I'm running Mandrake 10.0 with Apache 2 (installed from Mandrake RPM) with mod_ssl. I've installed a test certificate and it worked great, just trying to close out the possibility of http:// now. (without forcing people to type https:// each time)

    EDIT:Updated to fix my typo... http=>https not https=>https
     
    Last edited: 10 Jan 2005
  2. planki

    planki ...

    Joined:
    20 Dec 2003
    Posts:
    1,132
    Likes Received:
    0
  3. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Is it a PHP server?

    If so, this *may* work as a global include on any of your http:// pages

    Code:
    <?php
    
    if(substr($_SERVER['SERVER_PROTOCOL'], 0, 5) != 'HTTPS') {
        //header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        header('Location: https://'.$_SERVER['HTTP_HOST']);
    }
    
    ?>
    
    This is assuming that $_SERVER['SERVER_PROTOCOL'] changes to HTTPS/1.0 (or 1.1?) when you access https://

    (The commented section would send a user from http://server.com/request/path to https://server.com/request/path)

    Otherwise you might want to look into mod_rewrite which i'd assume can do the job just as well :)
     
  4. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Yeah, I need to see if I have mod_rewrite enabled on the server, because that may be the best bet. I'd rather stay away from a universal include, as not all of the pages are .php scripts.

    Anyone with a good mod_rewrite recipe?
     
  5. trigger

    trigger Procrastinator

    Joined:
    22 Mar 2004
    Posts:
    1,098
    Likes Received:
    31
    Call me crazy, and note I'm no expert in this field, but could you not just use a meta tag:

    Code:
    <meta http-equiv="REFRESH" content="10; URL=https://foo.example.com">
     
  6. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Well, if I used meta tags I would have to rely on the user's browser to respect the meta tag and actually refresh and go to the site. I would also have to include this on every page, and I'd rather redirect people without them even knowing, reguardless of what page they typed in.
     
  7. scoob8000

    scoob8000 Wheres my plasma cutter?

    Joined:
    17 Feb 2002
    Posts:
    1,947
    Likes Received:
    0
    It's possible in your apache.conf to setup a redirect for that. I'd give ya the specifics but I cannot search right now.. Just google apache redirect..
     
  8. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    I've found a couple of things so far, but nothing that says for-sure what it exactly would be doing.

    I'm just hoping to find something before trying it out on my live server.

    I'll let you know what I find.
     
  9. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    We have a winner!
    Code:
      RewriteEngine On
      RewriteCond %{SERVER_PORT} ^80$
      RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]
    
    Works like a charm, and even does all the crazy url rewriting I didn't need, so if someone changed their current page, however deep into the app they are, to http:// it will automagically change it to https.

    :D

    Thanks for the tips, I googled "apache rewrite http to https" and found something similar on someone's website. redirect was just a bad word I guess.
     
  10. scoob8000

    scoob8000 Wheres my plasma cutter?

    Joined:
    17 Feb 2002
    Posts:
    1,947
    Likes Received:
    0
    I knew I've done it before, just couldn't think of the right term.. :)
     
  11. xTCM

    xTCM What's a Dremel?

    Joined:
    27 Jan 2005
    Posts:
    1
    Likes Received:
    0
    ---.htaccess----
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)/.php$ https://%{SERVER_NAME}/$1 [R]
    -----------------

    i haf no idea what to put in the httpd.conf file. I tried redirecting from http to https but it doesnt work and give me a 404 error.(can only access https now)
    seriously need help :X
     
Tags:

Share This Page