views:

41

answers:

1

Hi!

I've had a hard timing learning rewrite rules in .htaccess files, so I was hoping someone could show me how to rewrite this:

test.com/home/feed/toak/nyheter/vg

into this:

test.com/index.php?r=home&f=feed;toak;nyheter;vg

This is a dynamic URL, and can have multiple elements seperated by ; in the end. I want my users to be able to type in as human friendly URLs as possible. I'm hoping someone can help!

+1  A: 

If you have variable number of parameters, you can't do it only with .htaccess. You could rewrite in php:

RewriteEngine On
RewriteRule .* rewrite.php [NE,L]

And parse $_SERVER['REQUEST_URI'] in rewrite.php.

For static number of 4 variables:

RewriteEngine On
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+) index.php?r=$1&f=$2;$3;$4;$5

Or you could do (for any number of variables):

RewriteEngine On
RewriteRule ^(\w+)/(.*) index.php?r=$1&f=$2

And use $_GET['f'] = strtr($_GET['f'], '/', ';'); in index.php.

Mewp
Okai, but how do I do this in rewrite.php with a dynamic number of variables?
toak
`$vars = explode('/', $_SERVER['REQUEST_URI']); $r = array_shift($vars); $f = $vars;`
Mewp
Okai, thank you very much!
toak