无限重定向循环

时间:2014-03-11 17:09:59

标签: php .htaccess

我必须完成别人的项目,但我对无效请求有疑问:

如果我把这个网址设为有效:

http://localhost/Files/pictures/thumbs/28967579qwR.jpg_2014-03-11%2014-13-53.jpg

有了这个,我有无限循环(文件不存在):

http://localhost/Files/pictures/thumbs/s28967579qwR.jpg_2014-03-11%2014-13-53.jpg

有了这个,我没有重定向循环,但我问的是一个不存在的文件:

http://localhost/home/sd.jpg

在.htaccess中我有这个:

Options -Indexes
Options +FollowSymlinks
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [L,QSA]

我的index.php文件:

<?php

/*
 * Url friendly base
 */

$url = $_SERVER['REQUEST_URI'];
error_log($url);
if (isset($url)) {
    $links = explode('/', $url);
    //voler minuscula
    $link = strtolower($links[1]);
    switch ($link) {

        case 'join':
            include_once 'frontend/join.php';
            break;

        case 'join-video':
            include_once 'frontend/join-video.php';
            break;

        case 'hd-movies':
            include_once 'frontend/hd-movies.php';
            break;

        case 'contact':
            include_once 'frontend/contact.php';
            break;

        case 'login':
            include_once 'frontend/login.php';
            break;

        case 'model':
            include_once 'frontend/model.php';
            break;

        case 'our-models':
            include_once 'frontend/our-models.php';
            break;

        case 'recherche':
            include_once 'frontend/resultat-recherche.php';
            break;

        case 'video':
            include_once 'frontend/video.php';
            break;
        case 'update':
            include_once 'frontend/updates.php';
            break;

        case 'home':
            include_once 'frontend/index.php';
            break;
        default:
            header('Location: home/');
            // include_once 'frontend/index.php';
            break;
    }
}
?>

您是否知道为什么会在某些文件上发生这种情况而不是其他文件以及如何修复它?感谢。

1 个答案:

答案 0 :(得分:0)

为了避免无限循环,我最终决定使用会话:

    <?php

session_start();
if (!isset($_SESSION['redirect_uri'])) {
    $_SESSION['redirect_uri'] = 0;
}

$url = $_SERVER['REQUEST_URI'];
if (isset($url)) {
$links = explode('/', $url);
//voler minuscula
$link = strtolower($links[1]);
switch ($link) {

    case 'join':
        include_once 'frontend/join.php';
        break;

    case 'join-video':
        include_once 'frontend/join-video.php';
        break;

    case 'hd-movies':
        include_once 'frontend/hd-movies.php';
        break;

    case 'contact':
        include_once 'frontend/contact.php';
        break;

    case 'login':
        include_once 'frontend/login.php';
        break;

    case 'model':
        include_once 'frontend/model.php';
        break;

    case 'our-models':
        include_once 'frontend/our-models.php';
        break;

    case 'recherche':
        include_once 'frontend/resultat-recherche.php';
        break;

    case 'video':
        include_once 'frontend/video.php';
        break;

    case 'update':
        include_once 'frontend/updates.php';
        break;

    case 'home':
        include_once 'frontend/index.php';
        break;

    default:
        // This will avoid redirect loop
        $_SESSION['redirect_uri']++;
        if ($_SESSION['redirect_uri'] < 1 ) {
            header('Location: home/');
        } else {
            $_SESSION['redirect_uri'] = 0;
            header("HTTP/1.0 404 Not Found");
        }
        break;
}

} ?&GT;