Forum Moderators: phranque

Message Too Old, No Replies

Rewrite from http to https issues

         

ControlZ

4:48 am on Dec 13, 2011 (gmt 0)

10+ Year Member



I am looking for some assistance with a rewrite rule. I want to redirect users who access a link to a secure page (site is coded with https for these links but I am concerned that users that discover these pages via a search directory will land on an un-secure page.

Here is the rule I have written:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^shop\/giftcards\.php$ "https\:\/\/www\.thisdomain\.com\/shop\/giftcards\.php" [R=301,L]

The above returns an error message that the redirect will never work.

lucy24

5:31 am on Dec 13, 2011 (gmt 0)

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



Well, that's reassuring.

User requests /shop/giftcards.php in some domain or other.

They get redirected to
[thisdomain.com...]

Because of the way redirects work, this sends them all the way outside and then they come back, now asking for
/shop/giftcards.php

within
www.thisdomain.com

Does this fit the shop/giftcards.php rule and the ^.*$ condition? Ay-yup, says mod_rewrite, it does. So redirect them to
[thisdomain.com...]

Because of the way redirects work, this sends them all the way outside and then they come back, now asking for
/shop/giftcards.php

within
www.thisdomain.com

Does this fit the shop/giftcards.php rule and the ^.*$ condition? Ay-yup, says mod_rewrite, it does. So redirect them to
[thisdomain.com...]

Because of the way redirects work, this sends them all the way outside and then they come back, now asking for
/shop/giftcards.php

within
www.thisdomain.com

Does this fit the shop/giftcards.php rule and the ^.*$ condition? Ay-yup, says mod_rewrite, it does. So redirect them to
[thisdomain.com...]

Are you getting the idea? ;)

ControlZ

2:45 pm on Dec 13, 2011 (gmt 0)

10+ Year Member



I see what you are saying. What would be the correct way to approach this?

g1smd

4:43 pm on Dec 13, 2011 (gmt 0)

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



Use a RewriteCond to test %{SERVER_PORT} to see if the request was NOT on port 443. If true, redirect.

Slashes do not need to escaped in RegEx patterns.

Nothing needs to be escaped in the rule target.

lucy24

10:00 pm on Dec 13, 2011 (gmt 0)

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



Nothing needs to be escaped in the rule target.

I was scratching my head over that too, but decided one answer was enough. I'm surprised the target wasn't interpreted as (literal text)

www\.thisdomain\.com\/shop\/giftcards\.php

which would probably never get past the DNS. And if it did, it would land in a 404 rather than an infinite loop. Nice of mod_rewrite to read your mind and know you didn't mean it about all the backslashes ;)