Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rerouting all php requests through index.php

Tags:

php

.htaccess

How can I reroute all requests for php pages through index.php?

My .htaccess is as follows:

Options +FollowSymLinks
IndexIgnore */*
#Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ index.php%{REQUEST_URI} [L]

Basically, I'm trying to emulate .NET master pages. The index.php has the site header/footer.

It redirects 404 to index.php. How can I make it redirect all requests to php pages (except index.php itself)?

Is there any performance issues with this method (don't want to use a framework)?

like image 426
firebird Avatar asked Mar 14 '12 00:03

firebird


1 Answers

Here's what I use (and have used for ages):

<IfModule mod_rewrite.c>
    # Redirect /index.php to / (optional, but recommended I guess)
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
    RewriteRule ^index.php/?(.*)$ $1 [R=301,L]

    # Run everything else but real files through index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
</IfModule>

As the comments suggest it will route every request that isn't an actual file to index.php

like image 125
powerbuoy Avatar answered Oct 03 '22 18:10

powerbuoy