views:

269

answers:

2

I only recently found out about URL rewriting, so I've still got a lot to learn.

While following the Easy Mod Rewrite tutorial, the results of one of their examples is really confusing me.

RewriteBase /
RewriteRule (.*) index.php?page=$1 [QSA,L]

Rewrites /home as /index.php?page=index.php&page=home.

I thought the duplicates might have had been caused by something in my host's configs, but a clean install of XAMPP does the same.

So, does anyone know why this seems to parse twice?

And, to me this seems like, if it's going to do this, it would be an infinite loop -- why does it stop at 2 cycles?

+2  A: 

Seems like they explain it here: http://www.easymodrewrite.com/notes-last under "Example 1"

David Zaslavsky
A: 

From the Apache Module mod_rewrite documentation:

'last|L' (last rule)
[…] if the RewriteRule generates an internal redirect […] this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

To prevent this you could either use an additional RewriteCond directive:

RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) index.php?page=$1 [QSA,L]

Or you alter the pattern to not match index.php and use the REQUEST_URI variable, either in the redirect or later in PHP ($_SERVER['REQUEST_URI']).

RewriteRule !^index\.php$ index.php?page=%{REQUEST_URI} [QSA,L]
Gumbo