我正在使用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 放在 URL 中时,它都会重定向到 www.test.com/ge.php问题是我必须为 GE 执行 301 重定向.php这意味着每当 URL 中 www.test.com/gen.php 时,它都会重定向到 www.test.com/bima.php。www.test.com/bima.php 需要重定向到 www.test.com/gen.php,反之亦然。有什么想法吗?或者无论如何要这样做?
您的重定向规则
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;
}
文件:比马.php
if($_SESSION['redirected']['from'] != 'ge.php') {
header("location: ge.php");
$_SESSION['redirected']['from'] = 'ge.php';
exit;
}
bima.php
重定向到ge.php
并ge.php
重定向到 bima.php
。这将导致无限循环。你想实现什么?为什么要ge.php
重定向到bima.php
,反之亦然?
无论哪种方式,您都应该更改您的设计。
这是一个解决方案,尽管我同意大家的观点,即基本概念有点古怪:
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]
通过使用重定向设置环境变量,第二个重定向不会触发,因为它会看到已设置重定向规则。 但是,不确定这是否会在最初成功后搞砸重定向。别人知道吗?