qwertymk
qwertymk

Reputation: 35284

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

Upvotes: 0

Views: 171

Answers (2)

dldnh
dldnh

Reputation: 8951

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

https://www.php.net/manual/en/function.stristr.php

Upvotes: 1

Grzegorz Łuszczek
Grzegorz Łuszczek

Reputation: 548

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

Upvotes: 2

Related Questions