.htaccess http to https (I need a ninja here!)

Discussion in 'Apache' started by ozrocks22, Feb 4, 2012.

  1. #1
    Hi!

    This same question has been posed about a billion times in just as many forums and I can't seem to find a clean workaround hence I really need an apache ninja who can answer this htaccess question.

    Problem: I need the htaccess to manage the following:

    1. redirect all non www traffic to www (i.e. http://mydomain.com becomes http://www.mydomain.com)
    2. I need traffic from the static ip address of the server routed to the http://www address (i.e. 192.192.192.192 becomes http://www.mydomain.com)
    3. I need HTTPS forced in 2 directories not files (i.e. http://www.mydomain.com/ssldir/ becomes https://www.mydomain.com/ssldir/)
    4. I need all other files & domains (except those specified above) to be http

    Here is what I have so far. This works (for the most part) but will not comply with step 4 above. Example - if i clicked from one of the specific SSL directories onto the root domain, I would still have https. If I clicked onto a non SSL directory, it would have SSL:

    ***BEGIN***

    #Force SSL on ssl-one directory
    RewriteEngine on
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{REQUEST_URI} ssl-one
    RewriteRule ^ssl-one/(.*)$ https://www.mydomain.com/ssl-one/$1? [L,R=301]

    #Force SSL on ssl-two directory
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{REQUEST_URI} ssl-two
    RewriteRule ^ssl-two/(.*)$ https://www.mydomain.com/ssl-two/$1? [L,R=301]

    #Redirect static IP to URL
    RewriteCond %{HTTP_HOST} ^999.999.999.999$
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

    #Redirect non WWW traffic to WWW
    RewriteCond %{HTTP_HOST} ^mydomain.com
    RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]


    ***END***

    I found that I can adhere to requirement 4 above ON DIRECTORIES ONLY if I use this code in there below the first two requests:

    #Disable SSL on non-ssl directory
    RewriteCond %{SERVER_PORT} 443
    RewriteCond %{REQUEST_URI} non-ssl
    RewriteRule ^non-ssl/(.*)$ http://www.mydomain.com/non-ssl/$1? [L,R=301]


    Why this can't work is there are a ton of directories and it wouldn't make sense to do a reqrite for each of them.

    SO - the question is, how do I accomplosh all of the above + make the HTACCESS file the most search engine friendly?
     
    ozrocks22, Feb 4, 2012 IP