简体   繁体   中英

.htaccess Redirecting Too Many Times Using Get Variables

I'm trying to redirect users from my main index page back to the index page but with a variable in the url to swap the loaded files. What I'm getting is too many redirects since I've added variables.

Here is what I've done:

 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]

This part works fine and redirects. However I've added a case switch function to my index page to eliminate the construction.php page altogether. So now I've made my redirect into this and it's redirecting too much.

 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]

The reason I am doing this is because I am going to have several redirects from construction to maintenance , 404 , etc.

What would cause this to keep redirecting too much when all I did was change the urls?

Just for reference here is the entire index.php file:

<?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>

You can't do that:

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

Because query string is not part of the test uri. Use instead:

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

You can omit the first line, If you use redirect=construction only for this page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM