PHP Redirect Index to Root

Recently I ran into a problem where I needed http://www.domain.com/index.php 301 redirected to http://www.domain.com/ and if you redirect index to ./ you will run into an infinite loop situation. Since root is index.php, 301 redirecting using header() will just call index.php again, and on reload it’ll do the same thing. Using REQUEST_URI in PHP you can determine what the exact URL instead of looking up the absolute path which will always end up with index.php. My solution is below.

if($_SERVER['REQUEST_URI'] == "/index.php") {
	header("HTTP/1.1 301 Moved Permanently");
	header("location: http://YOUR-DOMAIN-NAME.COM/"); 
}

Very easy alternative if you don’t have .htaccess (like on Windows Servers).

Comments

Leave a Reply