Lewisandclark 200
Lewisandclark 200

Reputation: 41

REGEX and RewriteRule

I'm hoping to gain some insight on how best to set up a whole lot of URL redirects. Because the URLs use a dynamic id, a regular Redirect permanent doesn’t seem to work for us. So I've tried a rewrite rule, which seems to work.

We're hoping to clean up a few strings like this:

/myolddirectory/olddir/oldfile.php?id=2 should redirect to: /mynewdirectory?id=2
/myolddirectory/olddir/oldfile.php?id=4 should redirect to: /mynewdirectory?id=4

This works:

RewriteCond %{QUERY_STRING} (^|&)id\=2($|&)
RewriteRule ^myolddirectory/olddir/oldfile\.php$ /attra-pub-summaries?id=2 [L,R]
RewriteCond %{QUERY_STRING} (^|&)id\=4($|&)
RewriteRule ^myolddirectory/olddir/oldfile\.php$ /attra-pub-summaries?id=4 [L,R]

We have some 600 ids. I'm just wondering if there's a cleaner way to do this w/o having to write 600+ rules. Thank you.

Upvotes: 1

Views: 52

Answers (1)

revo
revo

Reputation: 48751

You could do it using one condition:

RewriteCond %{QUERY_STRING} \bid=([24])\b
RewriteRule ^myolddirectory/olddir/oldfile\.php$ /attra-pub-summaries?id=%1 [L,R]

%1 refers to value held by first capturing group in rewrite condition.

Upvotes: 2

Related Questions