Description
This table details the Apache variables available within php that provide access to the requested URL, that is the one that called the php script to run.
I originally put it together for personal use to aid in the creation and use of search engine friendly URLs and to note the differences between similar variables.
Variable Reference
- $_SERVER ['HTTPS']
-
If the page is requested through the HTTPS protocol this will be set to "on" (in lower case). Otherwise the variable will either be undefined or set to "off" (in any case).
- $_SERVER ['HTTP_HOST']
-
The server component of the URL. This will not necessarily be the defined server name (set in $_SERVER ['SERVER_NAME']) but could be an IP address or any accepted alias, and will include the port number if one was explcitly provided.
Example values:
- 127.0.0.1
- www.domain.com
- www.domain.com:8080
- $_SERVER ['REQUEST_URI']
-
The requested URI. Essentially everything after the server component.
Example values:
- /
- /page.html
- /search?q=term&size=10
- /path/file/parameter/terms
- $_SERVER ['QUERY_STRING']
-
An untranslated query string if one was provided, otherwise blank. Esessentially anything after the first question mark in the URI.
Example values:
- q=term&size=10
- $_SERVER ['PATH_INFO']
-
Where a URI does not map directly to a file, Apache's lookback facility allows it to be repeatedly parsed backwards, stripping off components until it matches a valid file name on the system. This variable contains the details that have been discarded. That is it is the section of the the URI between the actual file and the query string if present, or the end of the URI if not.
This provides the ability to create 'friendly' URLs, by passing parameters as though they were a part of the path structure rather than through a query string. As well as making for more easily remembered and professional looking addresses, not all search engines will include pages featuring query strings.
If the URI matches a file without the need to lookback then this variable is not created.
Example values:
- /parameter/terms
- $_SERVER ['SCRIPT_NAME']
-
The part of the URI that maps to a file. This is almost like the $_SERVER ['REQUEST_URI'] variable with everything after the actual file name being stripped (including anything discarded by lookback), with the exception that where no file name is specified then the name of the file returned by the DirectoryIndex directive is appended.
Example values:
- /index.html
- /page.html
- /search
- /path/file
- $_SERVER ['PHP_SELF']
-
The URI excluding any query string and including any file names appended by a DirectoryIndex directive. This variable can be seen as a combination of the $_SERVER ['SCRIPT_NAME'] and the $_SERVER ['PATH_INFO'] variables.
Example values:
- /index.html
- /page.html
- /search
- /path/file/parameter/terms
- $_SERVER ['DOCUMENT_ROOT']
-
This gives the local file system path to the document root for the web site.
Example values:
- /usr/local/apache/htdocs
- c:\inetpub\wwwroot
- $_SERVER ['SCRIPT_FILENAME']
-
This variable returns the full local file system path for the file to which the URI maps. It is almost like a combination of the $_SERVER ['DOCUMENT_ROOT'] and the $_SERVER ['SCRIPT_NAME'] variables but that it includes the full file name irrespective of content negotiation.
Apache has a MultiViews option that allows for content negotiation, which in essence allows for the supression of all, or part of, a file's extension. e.g. a request for the file "index.html" can match either "index.html.en" or "index.html.de" depending on the Accept-Language header sent by the requesting user agent. This is also useful in creating 'friendly' URLs by supressing file extensions. While the $_SERVER ['SCRIPT_NAME'] variable will return what was specified, this variable will return the actual name of the file chosen by negotiation.
Example values:
- /usr/local/apache/htdocs/index.html
- /usr/local/apache/htdocs/page.html
- /usr/local/apache/htdocs/search.php
- /usr/local/apache/htdocs/path/file.php
- c:\inetpub\wwwroot\index.html
- c:\inetpub\wwwroot\page.html
- c:\inetpub\wwwroot\search.php
- c:\inetpub\wwwroot\path\file.php
- $_SERVER ['PATH_TRANSLATED']
-
This variable is essentially a combination of the $_SERVER ['DOCUMENT_ROOT'] and the $_SERVER ['PATH_INFO'] variables if there is a $_SERVER ['PATH_INFO'] variable set. If the $_SERVER ['PATH_INFO'] variable is not set then it either returns the contents of the $_SERVER ['SCRIPT_FILENAME'] variable, or when running on Apache 2 with a php version of 4.3.2 or greater it is blank to correctly reflect the behaviour of the Apache environment.
The variable was intended for use with CGI scripts, where a relative file is passed as a parameter to a script. That 'parameter' would be combined with the path to the web root directory in order to map it to a local file that the script could then access and use in processing.
Example values:
- /usr/local/apache/htdocs/index.html
- /usr/local/apache/htdocs/page.html
- /usr/local/apache/htdocs/search
- /usr/local/apache/htdocs/parameter/terms
- c:\inetpub\wwwroot\index.html
- c:\inetpub\wwwroot\page.html
- c:\inetpub\wwwroot\search
- c:\inetpub\wwwroot\parameter\\terms
Practical Examples
The requested URL can be fully reconstructed with the following code fragment:
'http'.(isset ($_SERVER ['HTTPS']) && $_SERVER ['HTTPS'] == 'on' ? 's' : '').'://'.$_SERVER ['HTTP_HOST'].$_SERVER ['REQUEST_URI']
To get a local file name relative to the web root after parsing, to account for lookback and content negotiation:
substr ($_SERVER ['SCRIPT_FILENAME'], strlen ($_SERVER ['DOCUMENT_ROOT']))
Notes
Other Apache environment variables are set within php that are beyond the scope of this document.
For the purposes of this document a URL is taken to be the full address as entered into a web browser or other user agent. The URI is the part of this relative to the server; that is it is the URL excluding any protocol, machine, or port information.
The examples given are all based on the values that would be returned from the following URLs:
- http://127.0.0.1/ (where index.html is served as the DirectoryIndex)
- https://www.domain.com/page.html
- http://www.domain.com/search?q=term&size=10
- http://www.domain.com:8080/path/file/parameter/terms (where 'file' matches file.php)
On Apache 2 servers the $_SERVER ['PATH_INFO'] variable is no longer created by default, instead the AcceptPathInfo directive needs to be set to "On" in the server configuration to enable lookback.
Add a Comment
All details are optional, except the comment, of course. Email addresses will not be displayed but are gravatar enabled.