【问题标题】:PHP Redirect location with htaccess使用 htaccess 重定向 PHP 位置
【发布时间】:2010-03-28 19:58:05
【问题描述】:

在我拥有的页面之一是允许管理员的地方,但是,如果未设置会话,我会使用标题将它们重定向到 index.php 并且该方法有效。

如果我将 index.php 替换为 home 用于 htaccess 将其更改为 index.php 但它会在浏览器中出现错误

这行得通:

if(!isset($_SESSION['MEMBER'])){ header("Location: index.php"); }

这不起作用:

if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }

htaccess:

RewriteRule ^home$ index.php

Firefox 中的错误:

页面重定向不正确

Firefox 检测到服务器正在重定向此请求 以永远不会的方式解决 完成。

此问题有时可能是由于禁用或拒绝接受 cookie。

它有什么问题?如何让这种方法发挥作用?

【问题讨论】:

    标签: php session .htaccess redirect


    【解决方案1】:

    您将 index.php 重定向到您的 php 文件中的主页,并将主页重定向到 .htaccess 中的 index.php。 Firefox 告诉您存在无限重定向循环。

    编辑: index.php 和 home 是同一个东西,所以重定向到任何一个都会导致无限循环。你需要做这样的事情:

    #.htaccess
    RewriteRule ^home$ index.php        # public page
    RewriteRule ^members$ members.php   # member only page
    

    然后在 index.php 中

    # index.php
    if(isset($_SESSION['MEMBER'])){
        header("Location: members");
        exit;
    }
    

    【讨论】:

    • 所以我不能在 php 标头中使用 'home'?
    • 可以,只是不能重定向回自身。
    • @Wayne:您可以在 PHP 标头中使用 home,但请务必更改 $_SESSION['MEMBER'] 的值以防止无限循环。
    【解决方案2】:

    相反。以下是您在 .htaccess 重定向中携带 PHP 会话 ID 的方式...

    RewriteEngine On
    RewriteRule ^(.*)$ /home/  [QSA,L,E=REDIRECT_URL:$1]
    

    注意最后一点...[QSA,L,E=REDIRECT_URL:$1]

    【讨论】:

      【解决方案3】:

      您已经创建了一个无限循环。 当 PHP 执行以下代码时:

      if(!isset($_SESSION['MEMBER'])){ header("Location: home"); }
      

      它将浏览器重定向到/home。 但是在.htaccess 中,您将URL 重写为/index.php。然后再次使 PHP 执行上述 PHP 代码。

      【讨论】:

        猜你喜欢
        • 2017-02-16
        • 2016-05-07
        • 2016-03-02
        • 1970-01-01
        • 2021-04-01
        • 2018-07-15
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多