PHP Script to Redirect Up-One-Level

This script reliably redirects to the directory one level above the current location in the web server's path.

For example, if you use the script as an index file at

it will instantly redirect the browser to

I searched for a rock-solid way to redirect the browser up-one-level and didn't find anything quite like I expected to find. Then I searched some more, wrote this PHP script, and created this page to publish and document it.

License: You are free to do whatever you want with the script.

The Script

Here's the script:

  This redirects to the directory "one level up" in the document tree. At
  it redirects to
# build the URL
$Scheme = (@$SERVER['HTTPS']=='on' ||
           ? 'https' : 'http';
$DirName = dirname($SERVER['REQUEST_URI']);
$EndSlash = ($DirName == '/') ? '' : '/'; // except docroot
$RedirURL = $Scheme.'://'.$SERVER['HTTP_HOST'].$DirName.$EndSlash;
# redirect
header("Location: $RedirURL");

Alternate Version

This version is essentially the same. Minor differences:

  • Requires PHP 4.1 or newer. PHP 4.1 was released in 2001.
  • The text block is minimal.

Direct link: index2-php.txt     <-- right-click to download

/* index.php -- Builds up-one-level URL, then redirects the browser.  */
$Scheme = (@$_SERVER['HTTPS']=='on' || @$_SERVER['SERVER_PORT']==443)
          ? 'https' : 'http';
$DirName = dirname($_SERVER['REQUEST_URI']);
$EndSlash = ($DirName == '/') ? '' : '/'; // except docroot
$RedirUrl = $Scheme.'://'.$_SERVER['HTTP_HOST'].$DirName.$EndSlash;
header("Location: $RedirUrl");


Usually you would use it as a directory index file.

  1. Download the script.
  2. Name it index.php.
  3. Put it the directory.


No configuration is necessary. The script is reliable and compatible.

  • It auto-adapts to PHP variables that can change from one server to the next.
  • It auto-adapts to HTTP or HTTPS protocol.
  • It redirects to a full URL path without ".." (a relative path), so it's compatible with any browser.

Example Commands

 $ wget
 $ mv index-php.txt index.php


 $ wget
 $ mv index2-php.txt index.php


Why redirect up-one-level?

Most often you will use the script in directories where you want to deny a directory listing. Someone doing "URL hacking" is quickly and silently redirected up one level. The redirection is virtually instant.

What if my server is configured (e.g. using .htaccess to deny directory listings?

In that case, the script still works as an index file. Your visitor is redirected rather than shown a Forbidden or Listing Denied message (HTTP 403 status code).

What directories does it belong in?

Any web-accessible directory without an index file, probably. It's up to you.

Why append the trailing slash?

Directory paths end with a slash, so a slash is appended. The trailing slash avoids an unnecessary extra redirect. ( reference)

Why is there no closing tag?

A ?> closing tag is omitted intentionally. ( reference)


-- Hagan Fox

Page last modified on June 14, 2017
Powered by PmWiki