0

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

4
  • You redirect /index.php to / which means same thing Commented Mar 18, 2012 at 12:50
  • @safarov: yeah but it looks nicer in the url bar, and I need it for navigational reasons. Commented Mar 18, 2012 at 12:52
  • You can do it with .htaccess file too. If you want i can write a solution for u with htaccess Commented Mar 18, 2012 at 12:56
  • @safarov: sure but I need the rule to apply for subfolders too Commented Mar 18, 2012 at 12:57

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I got the same issue with REQUIEST_URI which is why I thought that ORIG_PATH_INFO might work
Ok, it seems to be working now, is there any way to also check for index.php/ and index.php//// ...?
RewriteRule ^index.php[/]+$ / [L] if you use RewriteEngine
Yes. rtrim( $url, '/' ) (i.e. remove trailing slashes) before you check if it ends in index.php.
1

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

4 Comments

Ok, it seems to be working now, is there any way to also check for index.php/ and index.php//// ...?
I guess for that I'd recommend using stristr to detect any url containing index.php or any variation of that us3.php.net/manual/en/function.stristr.php
yeah but it could contain that in the query string
yes, of course -- if you first reduce the url string by loopping off as many characters as $_SERVER['QUERY_STRING'] contains, you can account for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.