Forum Moderators: phranque

Message Too Old, No Replies

can't get redirectmatch to work

struggled with mod_rewrite for awhile, but giving up

         

stephenrs

9:08 am on Jun 21, 2005 (gmt 0)

10+ Year Member



there is an area of my site which is accessed via ssl, and all is working fine, except some of the ssl'ed pages contain links back to non-ssl'ed areas of the site AND since these links (which are generated from a CMS) are relative, they default to https. not only do i want the non-ssl'ed pages of the site to stay that way for performance reasons, but IE complains if those pages are accessed via https because some of the elements of the pages are "insecure".

i could hack the cms code to force the links which should be http to be sent to the browser that way, but i'd rather produce a more general solution.

i've pulled my hair out with mod_rewrite, and now i've found that RedirectMatch should be able to accomplish the same thing much more simply - but i'm also having trouble with that. both modules are available to me via .htaccess.

here's a simple example of something i can't make work:

#/.htaccess:

RedirectMatch 301 (.*)contact(.*) [google.com...]

at this point i'm not even worried about the substitutions - i just want any url that contains the string "contact" to redirect to google.

please tell me what i'm doing wrong. or any suggestions for how to handle my situation overall would be greatly appreciated.

thanks

stephenrs

10:10 am on Jun 21, 2005 (gmt 0)

10+ Year Member



ok i have found part of my stupidity. one thing that i didn't mention was that the string that i need to match is actually in the query string, not the url. i didn't realize there was a distinction with the directives. so i'm back to mod_rewrite...

what i want to do is something like this:

RewriteCond %{query_STRING} ^(.*)keyword(.*)
RewriteRule ^(.*) [mysite.com...] [R=301, L]

what i'd like to say here is: "any time a query string contains <keyword> force the use of http (port 80)", but this doesn't seem to be working.

any advice?...i feel like i'm getting close.

thanks.

stephenrs

11:05 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



just to conclude this converstaion with myself :)...sometimes, suprisingly the act of expressing your problems in writing clarifies them, and makes the solutions jump out at you.

for anyone else trying to do something similar, i ended up solving this like so:


RewriteCond %{query_STRING} (.*)keyword(.*)$
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*) http://mysite.com/$1 [R=301,L]

without the "RewriteCond %{SERVER_PORT} 443" an infinite loop can be generated...

cheers.

jd01

11:40 pm on Jun 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi stephenrs,

Welcome to WebmasterWorld.

Sorry for the delay in the response, many of the regulars are out of town and I have found myself BUSY this last week.

RewriteCond %{query_STRING} (.*)keyword(.*)$

If you are not needing to 'store' variables your condition would be much more efficient by using the implicit 'anything before' keyword 'and anything after'. The regular expression .* is inherantly GREEDY.

This is accomplished by leaving the opening and closing of the line off:

RewriteCond %{QUERY_STRING} keyword

if you would like to test the end of the line for the keyword, you could use this:

RewriteCond %{QUERY_STRING} keyword$

and for the beginning you should be able to use something like this:

RewriteCond %{QUERY_STRING} ^variable=keyword

Hope this helps.

Justin

stephenrs

11:57 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



thanks for getting back to me justin, and as i look at it, i see you're right..it is kind of overkill to use the (.*) so liberally.

i'll make the change.

thanks again.

stephen