15

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)?

hakre
  • 193,403
  • 52
  • 435
  • 836
firebird
  • 3,461
  • 6
  • 34
  • 45
  • 1
    On Mod-Rewrite: http://stackoverflow.com/questions/8440490/pretty-urls-in-php-frameworks - On Performance: http://stackoverflow.com/questions/8145128/htaccess-mod-rewrite-performance – Mike B Mar 14 '12 at 00:14

4 Answers4

21

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

powerbuoy
  • 12,460
  • 7
  • 48
  • 78
  • The main part looks good (and matches what I do), but I see no point in the first part that routes `/index.php` to `/`. Because in your `index.php` file (which handles **all** routes, including `index.php`), if you don't want to display content for `/index.php`, just 404.... – Evan Teran Mar 14 '12 at 00:30
  • 3
    The reason to include that is so that your home page isn't available on more than one URL. mysite.com/ and mysite.com/index.php shouldn't both exist as you may get penalized for duplicate content. – powerbuoy Mar 14 '12 at 00:34
  • You could throw in a 302 for `/index.php` to redirect back to `/`. – Brigand Mar 14 '12 at 01:24
  • 3
    Hmm, I believe 301 is appropriate in this case. mysite.com is the preferred address, not mysite.com/index.php. With a 302 we'd say that mysite.com/index.php is the preferred and mysite.com is only temporary. – powerbuoy Mar 14 '12 at 03:02
  • @powerboy, I am saying that the site won't be available at `index.php` anyway (if you want)... What will happen is the script will get sent to `index.php` (since it is an existing file) and you can get the originally requested URL in the PHP code. Which makes it easily uniquely identifiable in your code. So you can 302, 404, whatever you like to handle it without the extra 2 lines in your `.htaccess` file. – Evan Teran Mar 14 '12 at 04:52
  • Heck, in fact since you can get the original URL (`$_SERVER['REQUEST_URI']`), your rule can just be:`RewriteRule .* index.php [L]` – Evan Teran Mar 14 '12 at 04:55
8

Use:


    
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
        RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
    

    
        ErrorDocument 404 /index.php
    

Url sample: www.site.com/friend/josh/03-13-2012

So the $params var value:

friend/josh/03-13-2012

Only need explode() "/" so u get array with params:

array(
0 => friend
1 => josh
2 => 03-13-2012 
)
Kin
  • 128
  • 9
  • On apache 2.4 your answer worked for me with the next changes: on RewriteCond $1 ..... I wrote instead of $1% this: {REQUEST_FILENAME} . I also added END flag: [END,L,QSA] – besciualex Aug 31 '17 at 06:30
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index\.php$
RewriteRule .*\.php$ index.php?q=%{REQUEST_URI} [L]

Will catch all requests ending in .php that don't point to a file ending in index.php and redirect them to index.php in the get parameter q

bkconrad
  • 2,620
  • 3
  • 20
  • 30
  • 1
    If you're not going to have a static folder/domain, you should probably say rewrite non-files. If you don't want a file to be sent; remove the read permissions. That way existing css/js/whatever files will be sent. Imaginary files will be handled by the PHP script. – Brigand Mar 14 '12 at 00:28
  • 1
    +1 because I know you're right. This is what op asked for though. – bkconrad Mar 14 '12 at 00:31
1

To redirect all non-existent requests to /index.php , You can also use ErrorDocument and FallbackResource directives .

Try one of the following examples :

 ErrorDocument 404 /index.php


 FallbackResource /index.php

These directives are part of apache core modules and you dont need to enable mod-rewrite to use them.

If you are looking for a way to redirect all requests (including real files/dirs) then try the following Redirect

RedirectMatch ^/(?!index\.php).+)$ /index.php
Amit Verma
  • 40,709
  • 21
  • 93
  • 115