开发者

RewriteRule kindness

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 peopl开发者_开发百科e! If you wish, I will put your credits on stuff, and then I'll share it as opensource stuff.


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜