Custom 404 not found and so on…

December 18, 2007

We are familiar with 404 not found error. it happens when the requested file is not found. But we can customize this error message.

There are some other apache error codes. We can categorize few of them as follows

  • Client Request Errors
    • 400 – Bad Syntax
    • 401 – Authorization Required
    • 404 – Not Found – most common
  • Server Request Errors
    • 500 – Internal Server Error

To customize the error message we need to follow the following steps

  • First, create the HTML page (e404.php) that we want to use as your error message and save it into the home directory
  • Add the following line in the .htaccess file
    • ErrorDocument 404 /e404.php
  • These two files are stored in the home directory (/htdocs/e404.php, /htdocs/.htaccess)
  • Now lets test it . From the browser address bar request (http://localhost/invalidScriptName) for any script that are not eixsts physically and the custom messages will be shown that are used in the e404.php

Thanks
$haymoL
eXploRinG Apache


PHP as Apache module

December 18, 2007

We can use php.ini directives configuration settings such as error_reporting, display_errors, auto_prepend_file etc from apache configuration files (httd.conf or .htaccess file).

Lets we want to append a config file (ex. init.php) at the beginning of a script(index.php). So we need to add the following statement in our .htaccess file.


1. init.php sample file
<?
define("DOCUMENT_ROOT" , $_SERVER['DOCUMENT_ROOT']);
define("DISPLAY_ERROR" , 1);
?>


2. index.php sample file
<?
print_r(DOCUMENT_ROOT);
?>


3..htaccess file
#php_value name value
#Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.

php_value auto_prepend_file init.php

N.B. All the files needs to should be store in the same directory as we use htaccess which used as per directory level. So all the php scripts in that directory will prepend the init.php file.

Now we are ready to test the script.

There are lots of php ini directives that can be altered from apache module.

For more info please visit the php manual

Cheer$
$haymoL
eXplOrinG PHP


URL Manipulation using mod_rewrite

December 16, 2007

What It is
mod_rewrite is frequently called the “Swiss Army Knife” of URL Manipulation. mod_rewrite is used for rewriting and redirecting URLs dynamically, using powerful pattern matching.
The most common use of mod_rewrite is to make ugly URL most attractive. For example , say our URL looks like http://www.example.com/index.php?cmd=login , and we want to show the url in the address bar as http://www.example.com/login. To do this we need to use the following.

# First make the RewriteEngine On
RewriteEngine On
# Translate login to /index.php?cmd=login
RewriteRule ^login$ /index.php?cmd=login [L]

Installing mod_rewrite
Because of its popularity, mod_rewrite is now included with all common Apache distributions. We can verify if your Apache installation has the mod_rewrite module by looking for a file named mod_rewrite.so under the modules folder in our Apache installation directory.
To make sure open up the httpd.conf file in the Apache conf folder and find the following
#LoadModule rewrite_module modules/mod_rewrite.so
The initail # means it commented out , so to install the mod_rewrite remove the # and restart the Apache. Thats all for mod_rewrite installation.

Testing mod_rewrite
We can write rules in httpd.conf file or in .htaccess file to work for per directory basis. .htaccess is used for customised rules for any specific directory.
Lets create .htaccess file in htdocs folder. and a index.php file that basically dump the POST variable.

index.php file goes as follows
// index.php file that basically do nothing only dump the post variable for this example
$cmd = $GET['login'];
if($cmd == 'login')
{
echo " You are in login Page";
}
else
{
echo "Elsewhere";
}
# First make the RewriteEngine On
RewriteEngine On
# Translate login to /index.php?cmd=login
RewriteRule ^login$ /index.php?cmd=login [L]

Now test the above rule using the url : http://www.example.com/login and see the output.

For more info please visit : Apache Mod Rewrite Rule Doc