301永久重定向中的错误

时间:2012-05-03 09:12:20

标签: php .htaccess redirect

我正在使用htaccess进行301永久重定向。 我必须将bima.php重定向到ge.php.so我在htaccess中编写了以下代码

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

这可以正常工作..每当我把www.test.com/bima.php放入网址时,它会重定向到www.test.com/ge.php 问题是我必须为ge.php进行301重定向。这意味着每当www.test.com/gen.php在url中它将重定向到www.test.com/bima.php。 www.test.com/bima.php需要重定向到www.test.com/gen.php,反之亦然。 有什么想法吗?或者无论如何要做到这一点?

3 个答案:

答案 0 :(得分:1)

您的重定向规则

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

重定向到无限循环。删除其中一个。

无论您使用何种类型的逻辑,您尝试的重定向都将一次循环结束。所以最好避免需要这样的要求。


这是一个PHP解决方案

文件: ge.php

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

文件: bima.php

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

答案 1 :(得分:0)

bima.php重定向到ge.phpge.phpbima.php。这将导致无限循环。你想要实现什么目标?为什么您希望ge.php重定向到bima.php,反之亦然?

你应该改变你的设计。

答案 2 :(得分:0)

这是一个解决方案,但我同意每个人的看法,基本概念有点古怪:

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

通过使用重定向设置环境变量,第二个重定向将不会触发,因为它将看到已设置重定向的规则。但是,不确定这是否会在初始成功后搞砸重定向。有人知道吗?