Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule help

switch from http to https and reversed

         

smartelements

9:38 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



Hi,
I have a simple problem but don't find the solution:

I want to redirect all urls containing: ?arg=login AND ?arg=order to https.

in clear words:

http ://www.domain.com?arg=login&other params..
http ://www.domain.com?arg=order&other params..

into

[domain.com?arg=login&other...] params..
[domain.com?arg=order&other...] params..

at the same time I want to to switch back from https into http if params do not match.

Thank you.

Thank you.

g1smd

9:44 pm on Feb 23, 2010 (gmt 0)

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



What have you tried so far?

It's a few lines of code for each of those, and a subject covered many times before.

smartelements

9:50 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



I tried following or similar:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/arg=login(.*)$ [domain.com...] [L,R]

g1smd

9:59 pm on Feb 23, 2010 (gmt 0)

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



RewriteRule cannot see query strings, use a RewriteCond to detect that.

R gives a 302 redirect. You'll need a 301 redirect.

You'll need another redirect "in reverse" to fix the other URLs.

smartelements

10:30 pm on Feb 23, 2010 (gmt 0)

10+ Year Member



I am not very familiar with rewrite syntax and tried to solve this with php scripts first.

Since there are too many other URLs (it is a webshop) I want to avoid to define a rewrite for each one. Shouldn't it be possible to define a single rule for all other parameters which do not match?

g1smd

10:59 pm on Feb 23, 2010 (gmt 0)

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



Yes, use ! for 'NOT'.

One rule says: if 'this' do this redirect.

The other says: if 'not this' do this other redirect.

You'll also need to sort out www and non-www redirects too.

smartelements

4:46 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Now I tried following lines

RewriteEngine on
RewriteCond %{QUERY_STRING} arg=order
RewriteRule (.*) [domain.com...] [QSA,L]
RewriteCond %{QUERY_STRING} arg=login
RewriteRule (.*) [domain.com...] [QSA,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule (.*) [domain.com...] [QSA,L]

switching to https for both params works now, but reverse not...

g1smd

8:18 pm on Feb 24, 2010 (gmt 0)

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



You'll also need to test for NOT port 443 on the rules redirecting to HTTPS.


On the rules redirecting to HTTP you'll need to test for NOT arg=order and NOT arg=login as well as testing the server port.


As I said above, the rules will be "mirror images" of each other. Whatever IS on one rule will be tested as NOT on the other rule.

You need just two rules, one for HTTP, and one for HTTPS. Each rule will have TWO preceding RewriteConds, one for SERVER_PORT and one for QUERY_STRING.

Use (order|login) to combine values into one rule.


As I also said above, you'll need the R=301 flag if you do not want a 302 redirect.

smartelements

8:40 pm on Feb 24, 2010 (gmt 0)

10+ Year Member



Thank you for the help.
Finally I got a working version

RewriteEngine on

RewriteCond %{QUERY_STRING} arg=login [OR]
RewriteCond %{QUERY_STRING} arg=order

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) [domain.com...] [QSA,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{QUERY_STRING} !arg=login
RewriteCond %{QUERY_STRING} !arg=order
RewriteRule (.*) http ://www.domain.com/ [QSA,L]

This works smooth, only on some browsers I get a message similar to "only show contents which are beeing sent over a https connection" even if all contents (pictures and links) are on the same server.

Do I need any refining?