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 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 in url it will redirect to
the problem is i have to do a 301 redirect for ge.php also.that means whenever in url it will redirect to needs to redirect to 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';

File: bima.php

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

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!