简体   繁体   中英

php redirect from /index.php to /

I'm trying to get the browser to redirect whenever the user types in /index.php or /index.php/ or /index.php//// to just plain / . Here's what I have so far

if ($_SERVER['ORIG_PATH_INFO'] == '/index.php') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://'.$_SERVER['HTTP_HOST'].'/'
}

But I get a infinite loop redirect error. What am I doing wrong? Is there any way to make this work?

EDIT, it seems that changing 'ORIG_PATH_INFO' to 'REQUEST_URI' did the trick for /index.php what do I add to my if to take care of index.php/ and index.php//// ...?

Use REQUEST_URI . When you use ORIG_PATH_INFO , you will always get /index.php .

base your condition on $_SERVER['REQUEST_URI'] instead and when the url doesn't actually contain index.php it won't try and redirect.

and use stristr to search for index.php wherever it occurs in the url

http://us3.php.net/manual/en/function.stristr.php

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