Skip to content

Instantly share code, notes, and snippets.

@thomas-p-wilson
Created March 2, 2011 19:42
Show Gist options
  • Save thomas-p-wilson/851572 to your computer and use it in GitHub Desktop.
Save thomas-p-wilson/851572 to your computer and use it in GitHub Desktop.
Determines the path and url to the current file. Handles HTTPS and mod_rewrite.
<?php
// Define root path constant. $_SERVER['SCRIPT_FILENAME'] seems to survive
// redirects.
$root_path = dirname($_SERVER['SCRIPT_FILENAME']);
if(substr($root_path,-1) != '/') {
$root_path .= '/';
}
define('ROOT_PATH',$root_path);
unset($root_path);
// Define root url constant
$root_url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$root_url .= $_SERVER['HTTP_HOST'] . '/';
$root_url .= substr(ROOT_PATH,
strpos(
ROOT_PATH,
$_SERVER['DOCUMENT_ROOT']
) + strlen($_SERVER['DOCUMENT_ROOT']) + 1);
define('ROOT_URL',$root_url);
unset($root_url);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment