Home .htaccess Redirecting Too Many Times Using Get Variables
Reply: 1

.htaccess Redirecting Too Many Times Using Get Variables

Jesse Elser
1#
Jesse Elser Published in 2017-11-14 17:46:36Z

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>
Croises
2#
Croises Reply to 2017-11-15 00:40:04Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314397 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO