Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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

@timniko

This comment has been minimized.

Copy link

timniko commented Mar 11, 2016

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...

@mrtdeh

This comment has been minimized.

Copy link

mrtdeh commented May 3, 2016

Thanks rav..very usefull for me.

@gautamdharmapuri

This comment has been minimized.

Copy link

gautamdharmapuri commented Jun 14, 2016

can I change the urls from gautam.com?State=MT to Montana.gautam.com?State=MT?

@gugglegum

This comment has been minimized.

Copy link

gugglegum commented Jul 17, 2016

Adding this line

RewriteRule . index.php [L]

causes an error "400 Bad request" when accessing any page. Adding leading slash before index.php solves this problem:

RewriteRule . /index.php [L]
@skizzo

This comment has been minimized.

Copy link

skizzo commented Apr 24, 2017

Very helpful! How can I filter out (/exclude) some specific paths where I don't want this to happen?
For e.g. http://www.example.com/:

Thanks!

@theitsloth

This comment has been minimized.

Copy link

theitsloth commented Jul 11, 2017

You can filter by writing for example :
Rewritecond %{REQUEST_FILENAME} !/api
will not rewrite example.com/api, but it will rewrite everything else.

@transparentDesign

This comment has been minimized.

Copy link

transparentDesign commented Jul 17, 2017

Works great, thanks :)

@orientdevelopers

This comment has been minimized.

Copy link

orientdevelopers commented Aug 31, 2017

how do the same in web.config

@sergibernad

This comment has been minimized.

Copy link

sergibernad commented Apr 9, 2018

If i want to rewrite for example www.example.com/hello/world to www.example.com/index.html?

@imjaypatel

This comment has been minimized.

Copy link

imjaypatel commented May 14, 2018

Hello, Following is my sub directory .htaccess rules. Its no longer work.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [L,QSA]
</IfModule>

Basically URL
https://website.com/subdirectory/a_tag

will result in
https://website.com/subdirectory/index.php?file=a_tag

Why its not work. Please let me know. Thanks

@ghost

This comment has been minimized.

Copy link

ghost commented May 26, 2018

Great Stuff!

Thanks for this.

@VyacheslavGL

This comment has been minimized.

Copy link

VyacheslavGL commented Jun 17, 2019

Здравствуйте, ниже приведены мои подкаталоги .htaccess правила. Его больше не работает.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [L,QSA]
</IfModule>

В основном URL
https://website.com/subdirectory/a_tag

приведет к
https://website.com/subdirectory/index.php?file=a_tag

Почему это не работает. Пожалуйста, дайте мне знать. Спасибо

Вот рабочий вариант!

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_URI}% !^/img/
RewriteCond %{REQUEST_URI}% !^/css/
RewriteCond %{REQUEST_URI}% !^/js/
RewriteRule ^(.*)$ index.php [L,QSA]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.