Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule and Condition help

         

sandeepnair85

5:43 am on Apr 21, 2010 (gmt 0)

10+ Year Member



Hi,

I have configured apache to use http and https. Now for some urls i have to redirect to https but i am a newbie and i dont know how to create rewrite rule and condition

My url is like

http://localhost/web/guest/home?p_p_id=58&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&saveLastPath=0&_58_struts_action=/login/create_account 


I want to redirect this url which has create_account in its query String to https.

Can anyone please help me?

Regards,
Sandeep

g1smd

6:12 am on Apr 21, 2010 (gmt 0)

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



You need two redirects.

One will redirect HTTP requests that should be HTTPS to HTTPS.

The other will redirect HTTPS requests that should be HTTP to HTTP.

Use a RewriteRule for each and add a negative match RewriteCond to examine SERVER_PORT before each rule.

The good news is that this question comes up several times every month so there are hundreds of prior examples. One was posted only days ago.

Please post your best-effort code as a basis for discussion.

sandeepnair85

7:08 am on Apr 21, 2010 (gmt 0)

10+ Year Member



Thank you sir for the reply

I have written a rewrite condition for http as

RewriteCond %{SERVER_PORT} =80
RewriteRule (i m not sure) [%{SERVER_NAME}$1...] [R,L]

I want a rewrite rule that identifies create_account from the parameter of the following url so that i can forward it to the corresponding https url
My current url is as below

http://localhost/web/guest/home?p_p_id=58&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&saveLastPath=0&_58_struts_action=/login/create_account

Thanks
Sandeep

[edited by: jdMorgan at 2:38 pm (utc) on Apr 21, 2010]
[edit reason] De-linked localhost URL [/edit]

jdMorgan

4:16 pm on Apr 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not clear what your server's DocumentRoot is, but assuming that "home" is a directory and not a file, and that this is a request for your site's "home page" with a query string attached, then in /web/guest/home/.htaccess:

RewriteCond %{SERVER_PORT} !=443
RewriteCond %{QUERY_STRING} ^([^&]*&)*_58_struts_action=/login/create_account(&.*)?$
RewriteRule ^$ https://%{HTTP_HOST}/ [R=301,L]

The query string will be passed through this rule as-is, and no further code changes are required to do so.

Delete your browser cache before testing any new code.

Jim

sandeepnair85

4:44 am on Apr 22, 2010 (gmt 0)

10+ Year Member



Hello sir,

Thank you for your reply. I could not get it to work with that. But thanks for the hint, i did something like this and its working

RewriteCond %{SERVER_PORT} !^443$ 
RewriteCond %{QUERY_STRING} struts_action=(create_account)*
RewriteRule ^(.*)$ https://%{SERVER_NAME}/%{REQUEST_URI} [NE,R,L]


Just a couple of question
if i write another rule inside that would it be an OR condition or AND condition?

How should i write a negative condition such that if the url is anything other than that it would be redirected to Http.

Regards,
Sandeep

g1smd

6:04 am on Apr 22, 2010 (gmt 0)

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



Use ! for logical NOT.

Be aware that you have created a 302 redirect. Change it to a 301 redirect.

The * on
struts_action=(create_account)*
allows a URL request like
example.com/struts_action=create_accountcreate_accountcreate_account
to be valid. Change the * to ? instead.

Each rule is a separate rule with its own RewriteCond conditions.