Search code examples
.htaccesshttp-redirecthttp-status-code-404forum

.htaccess RewriteRule to redirect url's containing specific parameters


I've seen several questions regarding similar requests, however, I'm not trying to redirect to a custom crafted URL. I just want to take common URLs that have been linked to and redirect them to a landing page.

For example, I want to catch a specific URL like the following, but only this URL:

http://example.com/forum/viewtopic.php?f=12&t=345

...and redirect it to:

http://example.com/landing-page.html

I have a few links which no longer exist, so I'll be adding 4 or 5 redirects to more useful pages since they're currently hitting 404's.

Thanks in advance!


Solution

  • Add this to your htaccess file in your document root:

    RewriteCond %{QUERY_STRING} ^f=12&t=345$
    RewriteRule ^forum/viewtopic.php$ /landing-page.html [R=301]
    

    Essentially you want to match the query string in this RewriteCond and the URI in the RewriteRule.