Leon Anavi
IT Tips && Tricks


Created: 20.08.2009 00:31 Last Modified: 20.08.2009 23:33 Views: 12111
Keywords: .htaccess, Apache, mod_rewrite, PHP

Forwarding All URLs to a Single File

The Goal: to Handle All URLs at a Single File

In order to provide better user experience, short URL and better indexing at search engines it is recommended to process all incomming URL requests at a single file. The .htaccess file has to be modified to fulfil this goal on Apache web server. All request for non-existing directories and files will be forward to a single file. This feature combined with appropriate parser can replace annoying long URL like http://www.example.com/index.php?page=contacts to http://www.example.com/contacts/.

Configuring .htacess File

At the beginning the mod_rewrite module must be started. Then all request for directories or files that do not exist have to be forwarded to a file. At the example a PHP file is used. The name of the requested item will be available at the argument query. The condition pattern is a standard Extended Regular Expression with some extras. The symbols -d and -f are used to mark directory and regular file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ manager.php?query=$1 [L,QSA]
A Parser has to be created at the PHP file to determine requests and serve them. To get the requested URL use the argument query or:
$sURL = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

Further Reading

Apache Module mod_rewrite

  Home | About | Contact | Disclaimer | Sitemap 2009-2022 Leon Anavi. All rights reserved.