November 16, 2008

Apache configuration journal - I

Redirect all requests for any resource to single file

Steps 1: Requires mod_rewrite

Steps 2: Add following line to <APACHE_HOME>/conf/httpd.conf if it doesn't exist.

LoadModule rewrite_module modules/mod_rewrite.so

Steps 3: Add following lines to .htaccess file in the root directory of your WEBROOT

We are asking apache to redirect all requests which don't have an existing source code to "index.php". This could have been any other existing file in the WEBROOT. This will also prohibit browsing of files in different folders. If you want to enable browsing for folders in your WEBROOT, modify the above .htaccess configuration to

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
</IfModule>

PS: This will however expose all the existing directories for browsing by others

However if you want to restrict access to particular type of files, use regex and following kind of configuration in .htaccess

<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>

The configuration above says don't serve any file starting in name with ".ht"

To enable php's default compression while delivering any content (Saves bandwidth)

<IfModule mod_php4.c>
php_value zlib.output_compression 16386
</IfModule>

No comments: