Forum Moderators: phranque

Message Too Old, No Replies

HTTP to HTTPS for only one section (folder) of the website

         

rmarescu

5:18 pm on Apr 27, 2006 (gmt 0)

10+ Year Member



Hello,

I want to force users to use HTTPS for one section (folder) of the website - /profile/.

First of all, I want to mention that I'm making redirects like this for all files and folders in web root:


RewriteRule ^profile(.+)?$ index.php?q=profile$1 [QSA,L]

The redirect can be done in 2 steps:
1. force them to HTTPS when they access this folder
2. redirect them back to HTTP when they leave the section.

Step1 it's easy:


RewriteCond %{HTTPS} =off
RewriteRule ^profile(.*) https://%{SERVER_NAME}/profile$1 [R=302,L]

For Step2, I tried this:


RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI}!^/profile(.*)$
RewriteRule (.*) http://%{SERVER_NAME}/$1 [QSA,L]

If I'm using only Step1, redirection from HTTP->HTTPS it's ok.
If I put Step2, I get this REQUEST_URI when I'm trying to go on /profile:


http://www.example.com/index.php?q=profile/

The order of the rules is:
Step 1
Step 2
Permanent redirects (for files&folders from web root)

Thanks!

jdMorgan

5:42 pm on Apr 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This rule should be last:

RewriteRule ^profile(.+)?$ index.php?q=profile$1 [QSA,L]

Jim

rmarescu

12:52 pm on May 5, 2006 (gmt 0)

10+ Year Member



Yes, this is the order I'm using, but it's not working.

jdMorgan

3:28 pm on May 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All I can suggest is a slightly-modified version:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^profile(.*) https://%{SERVER_NAME}/profile$1 [R=302,L]
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/profile
RewriteRule (.*) http://%{SERVER_NAME}/$1 [R=302,L]
#
RewriteRule ^profile(.+)?$ index.php?q=profile$1 [QSA,L]

This is just an alternate method with a couple of syntax tweaks; I don't see anything really wrong with your code. Some basic debug steps are:
  • Verify that the code is located in the filepath where it will be executed in both cases (Check the DocumentRoot config setting for HTTPS).
  • Use a server headers checker or something like Firefox's Web Developer extension to look at the server headers returned when you request HTTP and HTTPS pages in /profile and not in /profile, and 'follow' the redirects as they happen.

    Jim

  • rmarescu

    2:10 pm on May 17, 2006 (gmt 0)

    10+ Year Member



    With RewriteLog enabled, I found out what's going on.

    The logs when Step 2 (https->http) is disabled:


    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'profile/'
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (4) RewriteCond: input='80' pattern='!^443$' => matched
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (2) [per-dir F:/localhost/html/] rewrite profile/ -> https://www.example.com/profile/
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (2) [per-dir F:/localhost/html/] explicitly forcing redirect with https://www.example.com/profile/
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (2) [per-dir F:/localhost/html/] trying to replace prefix F:/localhost/html/ with /
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (1) [per-dir F:/localhost/html/] escaping https://www.example.com/profile/ for redirect
    [www.example.com/sid#24c6a0][rid#12d4a30/initial] (1) [per-dir F:/localhost/html/] redirect to https://www.example.com/profile/ [REDIRECT/302]
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'profile/'
    [www.example.com/sid#511528][rid#12e6f18/initial] (4) RewriteCond: input='443' pattern='!^443$' => not-matched
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'profile/'
    [www.example.com/sid#511528][rid#12e6f18/initial] (2) [per-dir F:/localhost/html/] rewrite profile/ -> index.php?q=profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) split uri=index.php?q=profile/ -> uri=index.php, args=q=profile/
    [www.example.com/sid#511528][rid#12e6f18/initial] (3) [per-dir F:/localhost/html/] add per-dir prefix: index.php -> F:/localhost/html/index.php
    [www.example.com/sid#511528][rid#12e6f18/initial] (2) [per-dir F:/localhost/html/] trying to replace prefix F:/localhost/html/ with /
    [www.example.com/sid#511528][rid#12e6f18/initial] (5) strip matching prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#12e6f18/initial] (4) add subst prefix: index.php -> /index.php
    [www.example.com/sid#511528][rid#12e6f18/initial] (1) [per-dir F:/localhost/html/] internal redirect with /index.php [INTERNAL REDIRECT]
    [www.example.com/sid#511528][rid#12eb460/initial/redir#1] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#12eb460/initial/redir#1] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'index.php'
    [www.example.com/sid#511528][rid#12eb460/initial/redir#1] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#12eb460/initial/redir#1] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'index.php'
    [www.example.com/sid#511528][rid#12eb460/initial/redir#1] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/index.php
    [www.example.com/sid#511528][rid#12fb738/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/test.png -> siir/test.png
    [www.example.com/sid#511528][rid#12fb738/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/test.png'
    [www.example.com/sid#511528][rid#12fb738/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/test.png -> siir/test.png
    [www.example.com/sid#511528][rid#12fb738/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/test.png'
    [www.example.com/sid#511528][rid#12fb738/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/test.png
    [www.example.com/sid#511528][rid#12ebfd0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#511528][rid#12ebfd0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#511528][rid#12ebfd0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#511528][rid#12ebfd0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/heading.php'
    [www.example.com/sid#511528][rid#12ebfd0/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/heading.php
    [www.example.com/sid#511528][rid#12d2a28/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#511528][rid#12d2a28/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#511528][rid#12d2a28/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#511528][rid#12d2a28/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/heading.php'
    [www.example.com/sid#511528][rid#12d2a28/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/heading.php

    Logs when all the rules are enable:


    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'profile/'
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (4) RewriteCond: input='80' pattern='!^443$' => matched
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (2) [per-dir F:/localhost/html/] rewrite profile/ -> https://www.example.com/profile/
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (2) [per-dir F:/localhost/html/] explicitly forcing redirect with https://www.example.com/profile/
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (2) [per-dir F:/localhost/html/] trying to replace prefix F:/localhost/html/ with /
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (1) [per-dir F:/localhost/html/] escaping https://www.example.com/profile/ for redirect
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (1) [per-dir F:/localhost/html/] redirect to https://www.example.com/profile/ [REDIRECT/302]
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'profile/'
    [www.example.com/sid#511528][rid#12deab8/initial] (4) RewriteCond: input='443' pattern='!^443$' => not-matched
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'profile/'
    [www.example.com/sid#511528][rid#12deab8/initial] (4) RewriteCond: input='443' pattern='^443$' => matched
    [www.example.com/sid#511528][rid#12deab8/initial] (4) RewriteCond: input='/profile/' pattern='!^/profile' => not-matched
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] add path info postfix: F:/localhost/html/profile -> F:/localhost/html/profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/profile/ -> profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'profile/'
    [b][i][www.example.com/sid#511528][rid#12deab8/initial] (2) [per-dir F:/localhost/html/] rewrite profile/ -> index.php?q=profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) split uri=index.php?q=profile/ -> uri=index.php, args=q=profile/[/i][/b]
    [www.example.com/sid#511528][rid#12deab8/initial] (3) [per-dir F:/localhost/html/] add per-dir prefix: index.php -> F:/localhost/html/index.php
    [www.example.com/sid#511528][rid#12deab8/initial] (2) [per-dir F:/localhost/html/] trying to replace prefix F:/localhost/html/ with /
    [www.example.com/sid#511528][rid#12deab8/initial] (5) strip matching prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#12deab8/initial] (4) add subst prefix: index.php -> /index.php
    [www.example.com/sid#511528][rid#12deab8/initial] (1) [per-dir F:/localhost/html/] internal redirect with /index.php [INTERNAL REDIRECT]
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'index.php'
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'index.php'
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (4) RewriteCond: input='443' pattern='^443$' => matched
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (4) RewriteCond: input='/index.php' pattern='!^/profile' => matched
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (2) [per-dir F:/localhost/html/] rewrite index.php -> http://www.example.com/index.php
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (2) [per-dir F:/localhost/html/] explicitly forcing redirect with http://www.example.com/index.php
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (2) [per-dir F:/localhost/html/] trying to replace prefix F:/localhost/html/ with /
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (1) [per-dir F:/localhost/html/] escaping http://www.example.com/index.php for redirect
    [www.example.com/sid#511528][rid#5983f28/initial/redir#1] (1) [per-dir F:/localhost/html/] redirect to http://www.example.com/index.php?q=profile/ [REDIRECT/302]
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'index.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'index.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (4) RewriteCond: input='80' pattern='^443$' => not-matched
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/index.php -> index.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'index.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/index.php
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/test.png -> siir/test.png
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/test.png'
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/test.png -> siir/test.png
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'siir/test.png'
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (4) RewriteCond: input='80' pattern='^443$' => not-matched
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/test.png -> siir/test.png
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/test.png'
    [www.example.com/sid#24c6a0][rid#126ec20/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/test.png
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (4) RewriteCond: input='80' pattern='^443$' => not-matched
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#1270cb0/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/heading.php
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] applying pattern '(.*)' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (4) RewriteCond: input='80' pattern='^443$' => not-matched
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] strip per-dir prefix: F:/localhost/html/siir/heading.php -> siir/heading.php
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (3) [per-dir F:/localhost/html/] applying pattern '^profile(.+)?$' to uri 'siir/heading.php'
    [www.example.com/sid#24c6a0][rid#12047f0/initial] (1) [per-dir F:/localhost/html/] pass through F:/localhost/html/siir/heading.php

    On the 2nd output, you can see in bold-italic lines where the REQUEST_URI it's changing to index.php.:


    ...
    [www.example.com/sid#511528][rid#12deab8/initial] (2) [per-dir F:/localhost/html/] rewrite profile/ -> index.php?q=profile/
    [www.example.com/sid#511528][rid#12deab8/initial] (3) split uri=index.php?q=profile/ -> uri=index.php, args=q=profile/
    ...

    After that, conditions from Step 2 will match, so it will redirect me back to http.

    I tried using THE_REQUEST on Step2, but this will affect other rules from my htaccess file:


    RewriteCond %{SERVER_PORT}!^443$
    RewriteRule ^profile(.*) https://%{SERVER_NAME}/profile$1 [R=302,L]
    #
    RewriteCond %{SERVER_PORT} ^443$
    RewriteCond %{THE_REQUEST}!profile
    RewriteRule (.*) http://%{SERVER_NAME}/$1 [R=302,L]
    #
    RewriteRule ^profile(.+)?$ index.php?q=profile$1 [QSA,L]

    jdMorgan

    4:18 pm on May 17, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    It seems to me that the problem is that after the redirect to HTTPS (first rule), the internal rewrite from "profile" to "index.php" (third rule) is invoked. The requested URI is now now longer equal to "profile" so the second rule is invoked, which redirects back to HTTP.

    To prevent that, exclude "index.php" from being redirected in the second rule:


    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^profile(.*) https://%{SERVER_NAME}/profile$1 [R=302,L]
    #
    RewriteCond %{SERVER_PORT} ^443$
    RewriteCond $1 !(profile[b]¦index\.php[/b])
    RewriteRule (.*) http://%{SERVER_NAME}/$1 [R=302,L]
    #
    RewriteRule ^profile(.+)?$ index.php?q=profile$1 [QSA,L]

    Change all broken pipe "¦" characters to solid pipes before use; Posting on this board modifies the pipe characters.

    Jim