Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've inherited ugly piece of php which must handle all requests via single index.php.

index.php generates different web-pages using smarty with embedded business-logic depending on wisited url.

How can I configure apache2/php5 to handle all requests via index.php?

UPDATE: are there any alternatives to mod-rewrite to use here?

share|improve this question
up vote 1 down vote accepted

OK, without mod_rewrite. mod_rewrite is the answer, of course, but this uses mod_alias:

RedirectMatch permanent ^/(?<!index\.php)(.*)

This should match anything that isn't preceded by index.php and issue a permanant redirect to the correct index.php. Note that you'll need to specify the full location as the target.

Updated I've tested the above and it needs Apache 2.0+ as it uses PCRE. If you need the actual path appending to index.php, then append $1 to the redirect path.

share|improve this answer
LoadModule rewrite_module     /usr/lib/apache/modules/

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?index.php$
RewriteRule . index.php
share|improve this answer
(path to mod_rewrite may be different, and may be on by default in apache2, I forget, this is copied from my apache 1.3 install) – Devin Ceartas Aug 19 '09 at 14:14

Save the following into a file named .htaccess in your web root.

# Turn on URL rewriting
RewriteEngine On

# Web Directory
RewriteBase /

# Protect certain folders from being viewed
RewriteRule ^(protected|directories) - [F,L]

# Rewrite URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
share|improve this answer

You may do this with a mod_rewrite rule.

#.htaccess code
RewriteEngine On
RewriteRule ^(.*)$ index.php [L]
share|improve this answer
I think you need the RewriteCond to avoid looping – Devin Ceartas Aug 19 '09 at 14:18
Likely. My mod_rewrite is rusty. – Boohbah Aug 19 '09 at 14:57

This is what works with my Wordpress install:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The two RewriteCond lines allow serving existing files: without them you'd have to pass images and stylesheets through index.php too.

share|improve this answer
+1. Thanks I'll try – Alex Bolotov Aug 26 '09 at 19:25

It is quite a bit more simple than any of these, and you are asking for a solution without mod_rewrite, so:

 Alias /static/  /path/to/static/files/
 Alias /         /path/to/script.php/

If you have no static files, you can, of course, omit the first line (but it usually answers the follow up of "how do I make exceptions").

As a dirty hack for older Apache (1.x), you can use the 404 handler and DocumentRoot an empty directory, but that's way ugly. I've seen it recommended before, but that was a long while ago when it was necessary. It might help in odd cases.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.