Skip to content

Instantly share code, notes, and snippets.

@RaVbaker
Created March 30, 2012 20:12
Show Gist options
  • Save RaVbaker/2254618 to your computer and use it in GitHub Desktop.
Save RaVbaker/2254618 to your computer and use it in GitHub Desktop.
[HOWTO] Rewrite all urls to one index.php in Apache

Redirect All Requests To Index.php Using .htaccess

In one of my pet projects, I redirect all requests to index.php, which then decides what to do with it:

Simple Example

This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

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

This enables the rewrite engine:

RewriteEngine on

This checks for existing folders (-d) and files (-f):

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

And this does the actual redirecting:

RewriteRule . index.php [L]

Extended Example

You can extend this to pass the requested path to the index.php file by modifying the RewriteRule to the following:

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The ^(.*)$ part tells the rewrite module that we want to pass down the whole requested path as one parameter. The QSA part tells the module to append any query strings to the request. The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter. You can extend this even further by using regular expressions. For example:

RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2

This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request

http://yourhost.com/some/path/somewhere

will result in

http://yourhost.com/index.php?first=some&second=path/somewhere

This allows for some creative ways to do clean URLs.

Trouble Shooting

If it's not working, make sure that mod_rewrite is installed on Apache. On a unix system you can just do

sudo a2enmod rewrite

to achieve that.

Source http://jrgns.net/content/redirect_request_to_index

@STerryWoods
Copy link

Is this what Wordpress uses to start the php from the Apache received request.

@Tammy06
Copy link

Tammy06 commented Oct 23, 2022

"This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:"

How do it direct ALL traffic to secure index.php, regardless of whether the files exist or not?

"This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:"
How do it direct ALL traffic to secure index.php, regardless of whether the files exist or not?

I'm also looking for this. Did you manage to solve it?

Removing both rewrite conditions should do it. that is
RewriteEngine on
RewriteRule . index.php [L]

@belfie13
Copy link

Just saw your code after I got the same result on my own... Thanks anyway! Do you know how to strip the .php extension from the url? Maybe it's not necessary, because I can link to page without extension, but anyway it would be nice to have. The snippets I found havent worked so far...

@timniko
RewriteRule ^(.+)\.php(.*)$ $1$2
I think.. let me know if you have any issues, should capture anything before and after .php

might need to add RewriteCond %{REQUEST_FILENAME} .+\.php.* before it to only rewrite on requests with .php in it but i havent tested it out yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment