Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem

redirect some php query string pages to another site

         

briggers

1:22 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Hi,

I am trying to set up a temporary redirection of some pages with urls like:

http://www.mysite.com/dir/index.php?p=21&x=123456789
http://www.mysite.com/dir/index.php?p=21
http://www.mysite.com/dir/index.php?p=26
http://www.mysite.com/dir/index.php?p=28&x=13456&y=789456

to a static page on another site like:
http://www.myothersite.com/index.html

Sometimes the initial url may have 2 or more querys, other times it will only have a single query, also the value for p can be anything but I want to redirect only on specific values like 21 or 26 or 28. They will all redirect to the same page.

I have tried a .htaccess file containing:

RewriteEngine on

RewriteRule ^dir/index\.php\?cPath=2[168].*$ http://www.myothersite.com/index.html [L,R=302]

but this did not work. Further reading suggests that I cannot test the query string in the RewriteRule but have to set RewriteCond like:

RewriteEngine on

rewritecond %{REQUEST_URI} ^.*index\.php.*$
RewriteCond %{QUERY_STRING} ^cPath=2[168]$
RewriteRule ^.*$ http://www.myothersite.com/index.html [L,R=302]

But that does nothing either.

What am I doing wrong?.....Please

Thanks
Richard

g1smd

1:36 pm on Jun 13, 2011 (gmt 0)

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



Never redirect to a URL ending in
index.html
. The canonical URL for the site root ends in a trailing slash.

The
REQUEST_URI
line is redundant if you make the main
RewriteRule
pattern
^([^/]+/)*index\.php$
here. This will test for request for index file in any folder or in root.

With the
^cPath=2[168]$
pattern in place with both start and end anchoring, the cPath parameter must be the ONLY parameter in the request. If you want other parameters to be present and the request still be redirected use
(^|&)cPath=2[168](&|$)
instead.

briggers

2:18 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Thank you sooo much g1smd. I've been trying to solve this one for hours and you have provided a solution that works. The htaccess file now contains:

RewriteEngine on

RewriteCond %{QUERY_STRING} (^|&)cPath=2[136](&|$)
RewriteRule ^([^/]+/)*index\.php$ http://www.myothersite.com/ [L,R=302]


Is there any way to stop it passing the cPath=2x as part of the othersite url - not that it matters too much, just curious now.

Richard

g1smd

2:30 pm on Jun 13, 2011 (gmt 0)

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



Yes. It's very simple and something that I nearly always forget to do - only discovering the problem on the first live test.

Add a question mark to the end of the target URL. This clears the query string.

If you don't clear the query string, you'll give the new page multiple URLs and that is not a good thing.

You said you wanted to redirect requests for
example.com/index.php?cPath=nn
here. Is there any possibility that anyone will request
example.com/?cPath=nn
from your site? The code does not redirect those requests, but should do. To fix that use:
 ^([^/]+/)*(index\.php)?$
instead.

briggers

3:16 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Yes. It's very simple and something that I nearly always forget to do - only discovering the problem on the first live test.

Add a question mark to the end of the target URL. This clears the query string.


It just gets better and better :)

Is there any possibility that anyone will request example.com/?cPath=nn from your site?


I think it's unlikely but just in case I'll use that - it does work, I've tried it.

Once again many thanks

g1smd

3:50 pm on Jun 13, 2011 (gmt 0)

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



This part of website stuff is where "attention to detail" becomes very important. You can kill your site by programming to "loose or poorly defined requirements".

In your case, the requirement is to redirect URL requets with "cPath=nn" as the only parameter, or as one of many parameters. You want it only for index.php requests and for filename missing (you don't want it if appended to image or stylesheet URLs, etc). You want it for both www and non-www requests (luckily it does that anyway).

That's a bit more detailed than the original question, and the sort of things you need to think about for every redirect job in the future.

The final part of the testing is to use the Live HTTP Headers extension for Firefox and test a variety of URLs that should and should not redirect. You are looking for the correct response, and for URLs that are redirected you are also checking you get from old to new URL in just one hop. That is, no request should result in a multiple step redirection chain.

I'm still not clear as to whether the parameter to be redirected is
p=
or
cPath=
as you used both in the original question. Likewise the numbers were 21, 26, 28 in the original question and the later code had 21, 23, 26 instead.