Robbert van den Bogerd
Robbert van den Bogerd

Reputation: 1034

Redirect in combination with a rewrite condition in htaccess

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]

Upvotes: 2

Views: 1971

Answers (2)

Gumbo
Gumbo

Reputation: 655269

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]

Upvotes: 3

Eric Petroelje
Eric Petroelje

Reputation: 60498

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]

Upvotes: 3

Related Questions