views:

21

answers:

1

Hi everybody. I'm trying to setup some small layout - template framework in php - and everything is going smooth except one thing - url rewriting.

I suck at regexp, and I suck even worse at mod_rewrite. And I'm in kinda hurry to get this stuff out, so can you please help me?

I need one .htaccess file that has just two rules

  1. Every request that ends with .png/.jpg/.gif/.js/.css handle as is. (eg: http://mysite.com/img/image.png > http://mysite.com/img/image.png)

  2. Everything else rewrite in following manner:

    http://mysite.com/foo/bar/44 > http://mysite.com/public/index.php?1=foo&2=bar&3=44 http://mysite.com/ > http://mysite.com/public/index.php

Many thanks people! If you wish, I will put your credits on stuff, and then I'll share it as opensource stuff.

+1  A: 

Try these rules:

RewriteRule .*\.(png|jpg|js|css)$ - [L]
RewriteRule !^/public/index\.php$ /public/index.php [L]

If you want to use these rules in an .htaccess file, remove the contextual path prefix from the pattern and substitution.

Note that the second rule does not rewrite the request to query parameters as you cannot count with mod_rewrite (or it would at least be very difficult).

But you could use a syntax that is interpreted as an array by PHP (name[]=value):

RewriteRule ^/public/index\.php$ - [L]
RewriteRule ^((/[^/]+)+)/([^/]+)$ $1?params[]=$3 [N,QSA]
RewriteRule ^/([^/]+)$ /public/index.php?params[]=$1 [L,QSA]

This allows any number of path segments.

But I would rather recommend my first suggestion and do the path parsing with PHP:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = array_filter(explode('/', $_SERVER['REQUEST_URI_PATH']), 'strlen');
Gumbo
I've just went to post out my dugout solution, and I saw this. I will post my somewhat incomplete solution, and accept yours as legit one.
Popara
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ public/index.php [QSA,L]
Popara