Forum Moderators: phranque

Message Too Old, No Replies

Redirect with multiple query string condition

         

zeon

5:50 pm on Feb 1, 2008 (gmt 0)

10+ Year Member



Hi,

I'm a novice to mod_rewrite and Webmasterworld.com. I've been trying to use mod_rewrite to force a specific link from http to https.

FROM
http://www.example.com/path/to/login/index.php?Object=Page&Name=Home.htm&login=true

TO
[example.com...]

Here is something I tried but some how it does not work...anything I'm missing here?

RewriteCond %{QUERY_STRING} ^Object=Page
RewriteCond %{QUERY_STRING} Name=Home\.htm
RewriteCond %{QUERY_STRING} login=true
RewriteRule ^/path/to/login/index\.php$ https://%{SERVER_NAME}/path/to/login/index\.php?Object=Page&Name=Home.htm&login=true [NC,L]

-zeon

jdMorgan

6:57 pm on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only thing I see wrong is that you didn't specify a 301 redirect. Also, if your name/value pairs are always in the same order, then you don't need three RewriteConds:

RewriteCond %{QUERY_STRING} ^Object=Page&Name=Home\.htm&login=true
RewriteRule ^/path/to/login/index\.php$ https://%{SERVER_NAME}/path/to/login/index\.php?Object=Page&Name=Home.htm&login=true [NC,R=301,L]

If this code is in .htaccess, remove the leading slash from the RewriteRule's pattern -- It won't be present in the URL-path 'seen' by rewriterule in an .htaccess context, so the rule will never match. If the code is in httpd.conf or conf.d, then the leading slash must be present, and your pattern is OK as-is.

If none of the above is helpful, please define "it doesn't work" and tell us how you tested, what results you got, and how those results differed from your expectations.

One more thing: If your https files are located in the same directory-path as this code, then you'll need an exclusion in the rule to prevent looping:


RewriteCond %{SERVER_PORT} !^443$

Jim

zeon

7:58 pm on Feb 1, 2008 (gmt 0)

10+ Year Member



It turns out you're correct about the leading /. I put these lines in httpd.conf and now it's working! Thanks.

About the 301 redirect and "RewriteCond %{SERVER_PORT}!^443$", are they really necessary since I have not include those two things and it is working fine...

-zeon

jdMorgan

8:49 pm on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> One more thing: If your https files are located in the same directory-path as this code, then you'll need an exclusion in the rule to prevent looping...

> About the 301 redirect and "RewriteCond %{SERVER_PORT}!^443$", are they really necessary since I have not include those two things and it is working fine...

If the conditions for my statement above are met, then check the redirect response with a server headers checker to be sure you aren't redirecting https to https until the browser or server gives up. I recommend the "Live HTTP Headers" add-on for Firefox/Mozilla-based browsers.

Jim

zeon

9:53 pm on Feb 1, 2008 (gmt 0)

10+ Year Member



Thanks for the tips. Everything seems working fine for now.

-zeon