Forum Moderators: phranque

Message Too Old, No Replies

Redirection of https to http

         

anoopsharda

3:24 am on Dec 23, 2006 (gmt 0)

10+ Year Member



hello

I have a problem. I have I am trying to redirect https url to http except for one page which has to be on https, all others need to be on http using following code

RewriteEngine on

RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ [sitename.com...] [R,L]

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^purchase.php$ [userid.powweb.com...] [R,L]

the problem is, when i click on purchase.php from http, it goes into somesort of loop and after sometime you get page cannot be displayed on the browser. First condition is working properly, but for second one i am getting problem. Can anyone help me with this.

Anoop

jonrichd

3:41 am on Dec 23, 2006 (gmt 0)

10+ Year Member



Try seting up your first rule as

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI}!^purchase.php$ [NC]
RewriteRule ^(.*)$ [sitename.com...] [R,L]

That will prevent the rewrite of purchase.php if it is called as https. Although it doesn't look like you are doing this, I wonder if because of the shared ssl you are using this is what is actually happening, causing the endless loop.

anoopsharda

3:51 am on Dec 23, 2006 (gmt 0)

10+ Year Member



Hi

Thanks, please advice if I should use this as the complete set of rewrite rules

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI}!^purchase.php$ [NC]
RewriteRule ^(.*)$ [sitename.com...] [R,L]

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^purchase.php$ [userid.powweb.com...] [R,L]

Anoop

jdMorgan

4:58 am on Dec 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fixing up a few missing spaces, escaping the literal periods in regular expressions, etc., something like this:

RewriteEngine on
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^purchase\.php$ [NC]
RewriteRule (.*) http://www.sitename.com/$1 [R=301,L]
#
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^purchase\.php$ https://userid.powweb.com/sitename.php [R=301,L]

Jim

[edited by: jdMorgan at 5:38 am (utc) on Dec. 23, 2006]

anoopsharda

2:34 am on Dec 24, 2006 (gmt 0)

10+ Year Member



hi

I am still facing problem.

When i try to go to https url, some how the site goes into loop and doens't come out and I get page not found error.

Can you help me.

Anoop

jdMorgan

3:40 am on Dec 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I see one more problem here. Try this change:

RewriteEngine on
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond [b]$1[/b] !^purchase\.php$ [NC]
RewriteRule (.*) http://www.sitename.com/$1 [R=301,L]
#
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^purchase\.php$ https://userid.powweb.com/sitename.php [R=301,L]

Jim

[edited by: jdMorgan at 3:41 am (utc) on Dec. 24, 2006]

anoopsharda

2:32 am on Dec 25, 2006 (gmt 0)

10+ Year Member



Hi

Jim, Thanks, it worked.

Anoop

ianevans

2:29 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Found this thread and combined it with another I found about directories and the supporting files like images...new to mod_rewrite but thought this would work in my SSL virtualhost:

RewriteEngine on
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond $1!(^securedir/¦\.(css¦gif¦jpe?g¦bmp¦js¦inc)$) [NC,OR]
RewriteCond $1!(^2ndsecuredir/¦\.(css¦gif¦jpe?g¦bmp¦js¦inc)$) [NC,OR]
RewriteCond $1!(^3rdsecuredir/¦\.(css¦gif¦jpe?g¦bmp¦js¦inc)$) [NC]
RewriteRule (.*) http://www.example.com$1 [R=301,L]

The rule worked fine in the non secure directories changing them to http if they were accidentally https. The problems started when I was in one of the three secure dirs and clicked a link or did a reload. I'd get an error message from firefox saying that the server had detected a redirect that it could never complete. Looking at live http headers I could see that it would try and grab the http version instead of the https.

jdMorgan

4:20 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you mean that your code is in your httpd.conf file for your SSL vhost, then this code won't work, because it's written for use in .htaccess. The RewriteRule must be changed subtly, because a leading slash will be present on URL-paths at the server-configuration level. Also, re-structuring and cleaning up the redundant regex:
 
RewriteEngine on
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond $1 !^(securedir¦2ndsecuredir¦3rdsecuredir)/ [NC,OR]
RewriteCond $1 !\.(css¦gif¦jpe?g¦bmp¦js¦inc)$ [NC]
RewriteRule [b]^/([/b].*) http://www.example.co[b]m/$1[/b] [R=301,L]

Change all broken pipe "¦" characters in the code above to solid pipe characters before use; Posting on this forum modifies the pipe characters.

After modifying and uploading this code, flush your browser cache (on IE, delete Temporary Internet Files) and re-start your server.

Jim

ianevans

7:45 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Strangely enough it's still looping. Here's the virtualhost (we're running behind a nginx reverse proxy, which isn't adding any rewrites of its own to the mix. It handles the SSL. This setup has been working fine for quite some time, so adding the rewrite has added this new twist):

<VirtualHost 127.0.0.1:8080>
ServerName www.example.com
Port 443
RewriteEngine on
#
RewriteCond %{SERVER_PORT} ^443$
RewriteCond $1!^(securedir¦securedir2¦securedir3)/ [NC,OR]
RewriteCond $1!\.(css¦gif¦jpe?g¦bmp¦js¦inc)$ [NC]
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]
</VirtualHost>

Here's the condensed result of Firefox's HTTP Live Headers WITHOUT the rewrite:

[example.com...]
GET /securedir/apc.php HTTP/1.1
HTTP/1.x 200 OK
----------------------------------------------------------
[example.com...]
GET /securedir/apc.php?IMG=2&1168543302 HTTP/1.1
HTTP/1.x 200 OK
----------------------------------------------------------
[example.com...]
GET /securedir/apc.php?IMG=3&1168543302 HTTP/1.1
HTTP/1.x 200 OK
----------------------------------------------------------
[example.com...]
GET /securedir/apc.php?IMG=1&1168543302 HTTP/1.1
HTTP/1.x 200 OK

As you can see, it grabs everything okay.

After the mod_rewrite is installed:

[example.com...]
GET /securedir/apc.php HTTP/1.1
HTTP/1.x 301 Moved Permanently
Location: http://www.example.com/securedir/apc.php
----------------------------------------------------------
http://www.example.com/securedir/apc.php
GET /securedir/apc.php HTTP/1.1
HTTP/1.x 301 Moved Permanently
Location: [example.com...]

And so the loop begins. Not quite sure why it's doing the 301 when securedir is present.

Thanks.

jdMorgan

7:49 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have port 8080 in this vhost's description, which is not port 443...

Jim

ianevans

7:56 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Yes, nginx is handling the SSL heavy lifting so Apache's working plain behind it. I guess I'll have to add the rewrite to the proxy, not the backend.

Too bad their rewrite isn't a drop in replacement...guess I have some translating to do.

OR...

Since you'd only be inside that virtualhost if you were coming through an SSL connection, can I just drop:

RewriteCond %{SERVER_PORT} ^443$

from the equation and still keep the rest?

jdMorgan

9:00 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or try replacing it with

RewriteCond %{HTTPS} ^on$ [NC]

Jim

ianevans

9:10 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Unfortunately, that didn't work.