.htaccess使用获取变量重定向太多次

时间:2017-11-14 17:46:36

标签: php .htaccess redirect mod-rewrite

我正在尝试将用户从主索引页面重定向回索引页面,但在url中使用变量来交换加载的文件。我得到的是重定向太多,因为我添加了变量。

以下是我所做的:

 RewriteCond %{TIME} <20171201000000
 RewriteCond %{REMOTE_ADDR} !^00\.000\.000\.000
 RewriteCond %{REQUEST_FILENAME} !(styles|images|javascript).+$
 RewriteCond %{REQUEST_URI} !construction.php$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|ttf|woff|js) [NC]
 RewriteRule .* /construction [R=302,L]

此部分工作正常并重定向。但是我已经在我的索引页面添加了case switch函数,以完全取消construction.php页面。所以现在我已经将我的重定向变为了这个,并且重定向太多了。

 RewriteCond %{TIME} <20171201000000
 RewriteCond %{REMOTE_ADDR} !^00\.000\.000\.000
 RewriteCond %{REQUEST_FILENAME} !(styles|images|javascript).+$
 RewriteCond %{REQUEST_URI} !index.php?redirect=construction$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|ttf|woff|js) [NC]
 RewriteRule .* /index.php?redirect=construction [R=302,L]

我这样做的原因是因为我将有几个重定向从constructionmaintenance404等。

当我所做的只是更改网址时,会导致这种情况导致重定向过多的原因?

仅供参考,这是整个index.php文件:

<?php include $_SERVER['DOCUMENT_ROOT'] . "/settings/config.php"; ?>
<body>
<?php include $_SERVER['DOCUMENT_ROOT'] . "/php/header.php"; ?>
<?php
    if (! isset($_GET['redirect']))
    {
        include($_SERVER['DOCUMENT_ROOT'].'/php/index.main.php');

    } else {    
        $redirect = $_GET['redirect'];  
        switch($redirect)
        {
            case 'construction':
                include($_SERVER['DOCUMENT_ROOT'].'/php/index.construction.php');
                break;  
            case 'maintenance':
                include($_SERVER['DOCUMENT_ROOT'].'/php/index.maintenance.php');
                break;  
        }
    }
?>
<div class="container-fluid">
<?php include $_SERVER['DOCUMENT_ROOT'] . "/php/footer.php"; ?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

你做不到:

RewriteCond %{REQUEST_URI} !index.php?redirect=construction$ [NC]

因为查询字符串不是测试uri的一部分。改为使用:

RewriteCond %{REQUEST_URI} !index\.php$ [NC,OR]
RewriteCond %{QUERY_STRING} !redirect=(?:construction|404)$ [NC] 

如果仅对此页面使用redirect=construction,则可以省略第一行。