Various URL Rewrites – PHP and Apache

URL rewriting is usually done to route request to a single application entry point such as index.php. Of course, it is also used to create human and bot friendly URLs. This type of URL rewriting comes in different flavor for different applications and frameworks (I’m talking about PHP). I will list URL rewrites for Zend Framework, Kohana and WordPress for future reference, who knows I get old and forget those rewriting styles.

Zend Framework

As of this writing, this is the URL rewrite for Zend Framework, taken from the command line tool, while generating a new project:

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Well, you can forget about the SetEnv, because they are sometimes not enabled by your host.

Kohana v3

Kohana v3 uses a bit different URL rewrite but will achieve the same result. Some settings are also included that are not really part of URL rewriting.

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /kohana/

# Protect hidden files from being viewed
<Files .*>
	Order Deny,Allow
	Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

The .htaccess rule also includes a configuration that will block access to files starting with dot (.) and also blocks access to Kohana’s vital directories.

WordPress

This is what I got on my old WordPress URL rewrite since 2.7 I think. Well, this could be the same as the lower / higher versions of course, this is their URL rewrite style.


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Almost the same as Kohana v3. Some settings are not included.

And here is another variant. I used this also for my Kohana installtion on a host that only got Apache 1.3.xx (that sucks).

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
This entry was posted in Apache, php and tagged , , , , . Bookmark the permalink.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *