0

What I need:

example.com/webpage/ should redirect to example.com/webpage

I am using:

$path = $_SERVER['REQUEST_URI'];
if(substr($path, -1) == '/')
{
    $path=rtrim( $path, '/\\' );
    header("Location: $path");
}

The problem:

When someone requests the homepage (with or without trailing slash), the value of $path becomes '/' and it causes a loop. Can anyone offer me a better solution?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42

1 Answers1

-1

Try

$path = $_SERVER['REQUEST_URI'];
$path = preg_replace('#/\s*$#', '', $path);
header("Location: $path");
Med
  • 2,035
  • 20
  • 31
  • This will _always_ redirect. Wouldn't that cause an infinite loop? `REQUEST_URI == '/'` results in an empty `Location:` header. Is that even valid? – Nisse Engström May 19 '15 at 17:21