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

 www.example.com/site/files/index.php

it will instantly redirect the browser to

 www.example.com/site/

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:

<?php
/*
  This redirects to the directory "one level up" in the document tree. At
     http://www.example.com/content/images/index.php
  it redirects to
     http://www.example.com/content/
*/
# build the URL
$SERVER = ($_SERVER) ? $_SERVER :  $HTTP_SERVER_VARS;
$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;
# 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

<?php
/* 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");

Installation

Usually you would use it as a directory index file.

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

Configuration

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 http://haganfox.net/site/uploads/Main.PhpRedirectUpOneLevel/index-php.txt
 $ mv index-php.txt index.php

or

 $ wget http://haganfox.net/site/uploads/Main.PhpRedirectUpOneLevel/index2-php.txt
 $ mv index2-php.txt index.php

Answers

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. (Apache.org reference)

Why is there no closing tag?

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

Enjoy.

-- Hagan Fox

Page last modified on June 14, 2017
Powered by PmWiki