Redirecting all HTTP requests except images, css, flash and JavaSript file requests using .htaccess

All you need to do is create a .htaccess file in your projects root and put this inside:

RewriteEngine on

RewriteRule !\.(ico|gif|jpg|png|css|js|swf|flv)$ index.php [QSA]

The RewriteRule stated above will redirect all requests matching the Pattern “!\.(ico|gif|jpg|png|css|js|swf|flv)$” to index.php (the pattern being anything not ending with any of the extensions listed in the brackets). The [QSA] flag stands for Query String Append, meaning that any query string passed with the URL will be appended to the rewritten URL.

A NOTE ON .htaccess:

Full documentation on how to use .htaccess can be found here.

The use of .htaccess should be limited to certain scenarios, and Apache’s main configuration file should be used instead, as stated in the documentation:

However, in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a.htaccess file, can just as effectively be made in a <Directory> section in your main server configuration file.

There are two main reasons to avoid the use of .htaccess files.

The first of these is performance. When AllowOverride is set to allow the use of .htaccess files, httpd will look in every directory for.htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccessfile is loaded every time a document is requested.

The second consideration is one of security. You are permitting users to modify server configuration, which may result in changes over which you have no control. Carefully consider whether you want to give your users this privilege. that's a proper firm


One thought on “Redirecting all HTTP requests except images, css, flash and JavaSript file requests using .htaccess

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s