Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

URL Rewriting Examples

Apache mod_rewrite

Rewriting with VirtualHosts

Here is a very basic virtual host definition, with the focus being the rewrite rules. These rules direct all requests to index.php, except specified file types (.js, .ico, etc.):

<VirtualHost my.domain.com:80>
    ServerName   my.domain.com
    ServerRoot   /path/to/server/root/
    DocumentRoot /path/to/server/root/my.domain.com/www

    RewriteEngine off

    <Location />
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
    </Location>
</VirtualHost>

Note the forward slash preceeding 'index.php'. These rules differ from .htaccess rules in that respect.

Rewriting with .htaccess

Routing requests

Again, these rules direct all requests to index.php, except specified file types:

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

Handling file and directory exceptions

These rules (used immediately prior to the RewriteRule above) exclude real files and directories from the rewriting and lets them pass through unaffected:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

You can also simply allow a specified group of files to pass through unaffected by using this line:

RewriteRule  ^(foo|bar).*  - [L]

In this case, files foo.* and bar.* will be accessed normally.

For more information, see Jayson Minard's Blueprint for PHP Applications: Bootstrapping.

ISAPI_Rewrite for Microsoft IIS

Please share your working example!

Lighttpd

url.rewrite-once = (".*\.(js|ico|gif|jpg|png|css)$" => "$0", "" => "/index.php")
Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.