Forum Moderators: phranque

Message Too Old, No Replies

redirecting http to https and vice versa

         

dslevin

2:23 am on Jul 9, 2009 (gmt 0)

10+ Year Member



I've recently come into a new project and I need to get certain pages to go from http to https while all others need to go to http. I've searched long and hard, including earlier post today, and tried so many combinations of rules, but I just can't get it to work. I can get all urls to rewrite to http but there are a few I need as https.

Current code is as follows. Also this .htaccess file is not in the main directory, but a directory named user-panel just below the main directory. ie mysite.com/user-panel.
I need the following page to redirect to https while all others rewrite to http.
[mysite.com...]

Currently all pages are being rewritten to http except the above which is uniquely being rewritten to this
[mysite.com...]

Please help.
PS. I did not write the other rewrite rules so I don't know how they are effecting the changes I need to make, or if the .htaccess file in the home directory might effect these rules.

Options FollowSymLinks Includes ExecCGI
RewriteEngine On
DirectoryIndex index.php index.html

# Externally redirect non-secure page requests from https to http, unless it is a specific page
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^.*onetime-credit-card$
RewriteRule ^(.*)$ [%{HTTP_HOST}%{REQUEST_URI}...] [L]

# Externally redirect page requests from http to https
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^.*onetime-credit-card$
RewriteRule ^(.*)$ [%{HTTP_HOST}%{REQUEST_URI}...] [L]

RewriteRule ^my-listings/my-(active¦expired)-listings/?(.*)/?$ index.php?p=my-listings/my-listings.php&listingStatus=$1 [QSA,L]
RewriteRule ^(my-clients)/manage-my-(buyers¦sellers)/?(.*)/?$ index.php?p=my-clients/mmb.php&clientType=$2 [QSA,L]
RewriteRule ^(listing-agent¦my-listings/edit-claim¦my-listings/edit-schedule)/([0-9]*)/?(.*)/?$ index.php?p=$1.php&id=$2 [QSA,L]
RewriteRule ^my-messages/view-(received¦sent)-messages/?(.*)/?$ index.php?p=my-messages/view-messages.php&messageType=$1 [QSA,L]
RewriteRule ^my-messages/(outbox¦inbox)/([0-9]*)/?(.*)/?$ index.php?p=my-messages/mailbox.php&box=$1&id=$2 [QSA,L]

RewriteCond %{REQUEST_URI} !(css¦library¦flash¦login¦js¦images¦testing¦ajax¦action¦fckeditor¦jscript)(/).*
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !(index¦check).php
RewriteRule ^(.*)$ index.php?p=$1.php [QSA,L]
RewriteRule ^(.*)/$ index.php?p=$1.php [QSA,L]

ErrorDocument 404 /index.php?p=error.php

jdMorgan

3:33 am on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This RewriteCond in the first rule looks wrong:
 RewriteCond %{REQUEST_URI} !^.*onetime-credit-card$ 

as does the RewriteCond in the rule that follows:
 RewriteCond %{REQUEST_URI} ^.*onetime-credit-card$ 

Because you later rewrite this URL, you will need to prevent that rewritten URL-path from being redirected back to http.

You might try
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /user-panel/onetime-credit-card\ HTTP/
in the first rule, and
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /user-panel/onetime-credit-card\ HTTP/
in the second rule.

This prevents these two rules from being 'fooled' by the later rewrite, and prevents the first rule from redirecting the rewritten https URL-path /user-panel/index.php?p=subscription-billing/onetime-credit-card.php back to http://example.com/user-panel/index.php?p=subscription-billing/onetime-credit-card.php

One thing that may not be obvious to you is that mod_rewrite in .htaccess is effectively recursive: If any rewriterule is invoked, then rule processing is re-started from the top. Checking the original client request is therefore useful to prevent problems when a redirect and a rewrite countermand each other.

Jim

dslevin

5:04 am on Jul 9, 2009 (gmt 0)

10+ Year Member



You're the man Jim, that fix worked. Thanks much!

I've been trying to study up on url rewriting but it is a hard nut to crack.

What is the purpose of the HTTP/ at the end of the above Rewrite condition?

Do you know of a good tutorial that explains all these small details?

Thanks again

jdMorgan

12:05 pm on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



THE_REQUEST contains the entire HTTP request line sent by the client, and exactly as you see it logged in your raw server access log -- for example:
GET /user-panel/onetime-credit-card HTTP/1.1

So the "\ HTTP/" at the end of the pattern simply assures that the requested path ended at "card" and didn't contain any additional path info. I refer to this as a "soft anchor," although that's just my personal term for it.

I'm not sure if you will find a tutorial that covers the situation that caused the problem here. It's really a result of the fact that because of the internal rewrite and the re-starting of mod_rewrite processing, this 'page' appears to have two paths inside the server, and you were only preventing a loop on one of them. An alternative solution would have been to test for both the requested and the rewritten path. However, the solution presented here seemed simpler to me.

For more general mod_rewrite tutorials, see the resources cited in our Apache Forum Charter, and the threads in our Apache Forum Library (See links at top of this page).

Jim

g1smd

8:04 pm on Jul 9, 2009 (gmt 0)

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



There are a LOT of Duplicate Content issues created with your code. The optional / allows at least two URLs to access the same content for every page of content.