簡體   English   中英

301永久重定向中的錯誤

[英]error in 301 permanent 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放在url中時,它將重定向到www.test.com/ge.php,問題是我還必須為ge.php做301重定向。只要網址中的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;
}

檔案: bima.php

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

bima.php重定向到ge.phpge.phpbima.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]

通過使用重定向設置環境變量,第二個重定向將不會觸發,因為它將看到已設置了重定向規則。 但是,不確定在最初成功后是否會破壞重定向。 有人知道嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM