mod_rewrite URL from unique query string URL to unique static URL

The original problem:

Redirecting [FONT=“Courier New”][COLOR=“Red”]http://www.old-domain.com/news.php?id=232[/COLOR][/FONT] to [FONT=“Courier New”][COLOR=“DeepSkyBlue”]http://www.new-domain.com/blog/unique-post-title-here/[/COLOR][/FONT]

I initially solved this by:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} id=232$
RewriteRule (.*) /blog/unique-post-title-here/? [L,R=301]
</IfModule>

…and then making a new condition and rule for each blog post redirect.

The new problem:

The solution above will target any URI with an ?id=232 (or whatever number) query string. So [FONT=“Courier New”][COLOR=“Red”]http://www.old-domain.com/[COLOR=“Black”][B]news[/B][/COLOR].php?id=232[/COLOR][/FONT] and [FONT=“Courier New”][COLOR=“Red”]http://www.old-domain.com/[COLOR=“Black”][B]staff[/B][/COLOR].php?id=232[/COLOR][/FONT] will redirect to the same place when they are obviously different pages.

How would you add the logic to filter out the URIs with specific text behind the query string?

By matching the URL in the RewriteRule :slight_smile:


RewriteEngine On
RewriteCond %{QUERY_STRING} id=232$
RewriteRule ^news\\.php$ /blog/unique-post-title-here/? [L,R=301]

^ That one will only match on news.php, not on staff.php

BTW. Get rid of the <IfModule …> and </IfModule>. Once you’ve established that mod_rewrite works there is no need to ask the server again and again for every request – you already know the answer, why ask the question? :slight_smile:

Worked like a charm - cheers! :smiley: