views:

187

answers:

2

Hi all!

I've got a cms-driven website, with no option to change the code. What I want to accomplish is creating friendly url's, using only apaches mod-rewrite engine.

The problem is I'm creating an infinite loop, because I first redirect the original url (index.php?id=21) to a friendly one (/friendly/) and then rewrite the '/friendly' part back to 'id=21'

I know there should be an extra condition or parameter to avoid looping in this case, but I can´t get one of the possible solutions to work.

Here´s the code:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]

+2  A: 

I'm guessing you are rewriting in both directions to ensure that old links are redirected to the new friendly urls?

You could just add a dummy parameter to all your "friendly" rewrites so that they don't match the other rule:

RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21&dummy=1 [L]
Eric Petroelje
Good suggestion, however this seems more like a hack then a proper solution. But hey, it works!
Robbert van den Bogerd
+1  A: 

You need to look at the request line in THE_REQUEST to see what originally has been requested:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?id=21\  [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]

RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]
Gumbo
I didn't know about the request line, but it seems this actually works. Just out of curiosity, what exactly makes that this code works and mine doesn't?
Robbert van den Bogerd
@Robbert van den Bogerd: When a rule is applied, the URI is changed. And the *L* flag implies a restart of the rewriting process with the new URI. That causes a infinite redirection as both rules create a new URI that is accepted by the other rule.
Gumbo