May 3, 2012

error in 301 permanent redirect

Question by asitha

i am using htaccess for 301 permanent redirect.
i have to make a redirect for bima.php to ge.php.so i wrote the below code in htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]

this works properly..whenever i put www.test.com/bima.php in url it will redirect to www.test.com/ge.php
the problem is i have to do a 301 redirect for ge.php also.that means whenever www.test.com/gen.php in url it will redirect to www.test.com/bima.php.
www.test.com/bima.php needs to redirect to www.test.com/gen.php and vice versa.
Any idea?or anyway to do this?

Answer by Starx

Your redirect rule

RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]
RewriteRule ^ge.php?$ $1/bima.php [NC,R=301,L]

Is redirecting in infinite loop. Remove one of them.

No matter what type of logic you use, the redirection you are attempting will end of in loop at one time. So better avoid the need of such requirement.


Here is a PHP Solution

File: ge.php

if($_SESSION['redirected']['from'] != 'bima.php') {
   header("location: bima.php");
   $_SESSION['redirected']['from'] = 'ge.php';
   exit;
}

File: bima.php

if($_SESSION['redirected']['from'] != 'ge.php') {
   header("location: ge.php");
   $_SESSION['redirected']['from'] = 'ge.php';
   exit;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!