Forum Moderators: phranque

Message Too Old, No Replies

Redirect Loop

Getting a loop with rewritten URL

         

punisa

1:20 pm on May 14, 2009 (gmt 0)

10+ Year Member



So I do my classic rewrite to a nice url:

RewriteRule ^(new-products)\/$ /products/newproducts.php? [L]

This all works nice, BUT somehow Google already indexed my /products/newproducts.php url. I guess it indexed it while I was still working on it, before the actual redirect.

So naturally I want to use a 301 redirect to a www.mysite.com/new-products/

I tried like this:


redirectMatch 301 /products/newproducts.php www.mysite.com/new-products/

And then it goes in a loop error.
I can see that it indeed does create a loop, but how can I resolve this?
Should I just redirect 301 via php for this one?

punisa

1:44 pm on May 14, 2009 (gmt 0)

10+ Year Member



update:
I changed this:

RewriteRule ^(new-products)\/$ /products/newproducts.php? [L]

To this:

RewriteRule ^(new-products)\/$ /products/newproducts.php?fr=$1 [L]

I added the GET code for rewritten part.
In PHP:


$fr = $_GET['fr'];
if($fr==""){
header("HTTP/1.1 301 Moved Permanently");
header("Location: www.mysite.com/new-products/");
exit();
}

Basically it checks if the page is already rewritten or not. If not, it redirects to a rewritten one by using 301 redirect.

No loop there, but how can I accomplish this only with .htacces code, without php?
Also, is "HTTP/1.1 301 Moved Permanently" ok to redirect single pages like that?

g1smd

10:38 am on May 21, 2009 (gmt 0)

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



You need a
RewriteCond
to check
THE_REQUEST
so that you only rewrite if the request was a direct client request.

Make sure that you also change your redirect to use a

RewriteRule
with the
[R=301,L]
flags.

That is, don't mix rules from Mod_Alias and Mod_Rewrite here.

There's hundreds of prior examples in this forum.

Also, you do not need to escape the slash in the pattern.