views:

24

answers:

1

Hi, I'm getting crazy with .htaccess and rewrite rule. I'd like to understand how it works, I hate it, anyway here's my problem.

(really simple for most of you)

My site has one main page index.php. This is the only page, all the others are handled by this one.

I did a simple RewriteRule:

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1

to do this:

/index.php?page=VAL -> /VAL

But I don't know how create a rule for this:

/index.php?page=VAL&var1=VAL2&var2=VAL3 etc.

I'd like a final URL like:

/VAL/VAL2/VAL3 etc.

Thanks

+1  A: 

You could just rewrite the request to your index.php and parse the requested URI path with PHP:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = implode('/', ltrim($_SERVER['REQUEST_URI_PATH'], '/'));

And the rule to that:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]

This will rewrite every request, that’s URI path can not be mapped to an existing file, to your index.php.

Gumbo
ok, if I understood well, the RewriteCond checks if the uripath isn't a file, but can you explain me what RewriteRule does? thanks
Marcx
@Marcx: The `RewriteRule` will rewrite any request, that’s URI path (without path prefix) is not `index.php`, to `index.php`.
Gumbo
ah thank you.. so `!` not `index.php` (path prefix) will be rewrite to `index.php` ok thank you so much..
Marcx
@Marcx: Oh yes, `!` at the begin negates the expression. So the rule is applied if `^index\.php$` does not match.
Gumbo