Forum Moderators: phranque

Message Too Old, No Replies

redirecting http to https and excluding non-secure from redirection

in continuation with a previous thread

         

crimsoni

11:21 am on Apr 22, 2010 (gmt 0)

10+ Year Member



while searching for the redirection, i came across this thread - [webmasterworld.com...]

however, my problem is that I have 2 secure directories. how do i use regex to include or exclude the two directories together.. any help will be greatly appreciated.

thanks

crimsoni

11:23 am on Apr 22, 2010 (gmt 0)

10+ Year Member



the thread ends with Jim's reply which I found very useful and did the job but did not address the problem i mentioned above... for those who do not want to visit the thread, jim's reply is here


After some off-line consulting, the simplified and more-efficient solution turned out to be like this, with all code in the root .htaccess file:

# Externally redirect all HTTP or HTTPS direct client requests
# for index.htm or index.html pages to HTTP home page "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.html?(\?[^\ ]*)?\ HTTP/
RewriteRule ^index\.html?$ http://www.example.com/ [R=301,L]
#
# Externally redirect all HTTPS requests for non-secure resources to HTTP
# except for js, css, and image files shared between SSL and non-SSL
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^secure-directory/
RewriteCond $1 !\.(js¦css¦gif¦jpe?g)$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect all HTTP requests for secure resources (files in
# the /secure-directory subdirectory) to HTTPS, except for the index
# page at /secure-directory/
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(secure-directory/.+)$ [example.com...] [R=301,L]
#
# Externally redirect all requests for non-blank, non-canonical hostnames to
# the canonical domain, preserving the original request's HTTP/HTTPS protocol
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦[0-9]+>s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]

Replace all broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

jdMorgan

12:18 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the above code works on your site with the sole exception of your second secure directory, then the problem is only one of modifying the regular expressions patterns, and should not affect the structure of the code itself.

Where the string "secure-directory" appears in the regex patterns above, replace it with "(secure-directory1|secure-directory2)" --including the parentheses exactly as shown-- and change the comments to cite "secure directory #1 or secure directory #2".

See our Apache Forum Charter for a link to a concise regular-expressions tutorial.

Jim

crimsoni

2:57 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Hi Jim.
Thats exactly what I did and it started working. Thanks for your comment though. I appreciate it.

however, i want to know how would this be written:

RewriteRule ^(secure-directory/.+)$ [example.com...] [R=301,L]

Will it be like this:

RewriteRule ^((secure-directory1 | secure2)/.+)$ [example.com...] [R=301,L]

?

g1smd

3:04 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where the string "secure-directory" appears in the regex patterns above, replace it with "(secure-directory1|secure-directory2)" --including the parentheses exactly as shown-- and change the comments to cite "secure directory #1 or secure directory #2".

Be sure to get the right 'pipe' symbol and don't add any spaces near it.