开发者

PHP Redirect location with htaccess

In one of the page I have is where administrators are allowed, however, I use if the session isn't set, the header will redirect them to index.php and that method works.

If I replace index.php with home which is for the htaccess which changes it to index.php but it gives an error in the browser

This works:

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

This does not work:

if(!isset($_SESSION['MEMB开发者_如何学PythonER'])){ header("Location: home"); }

htaccess:

RewriteRule ^home$ index.php

The error in Firefox:

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

What's wrong with it? How do I get this method to work?


You're redirecting index.php to home in your php file, and home to index.php in .htaccess. Firefox is telling you there is an infinite redirect loop.

Edit: index.php and home are the same thing, so redirecting to either will result in an infinite loop. You need to do something like this:

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

And then in index.php

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


Au Contraire. Here is how you carry PHP sessions id's within an .htaccess redirect...

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

Pay attention to the last bit... [QSA,L,E=REDIRECT_URL:$1]


You've created an infinite loop. When PHP executes the following code:

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

It redirects the browser to /home. But in .htaccess you rewrite the URL to /index.php. Which then again makes PHP to execute the above PHP code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜