To check for specific query strings you need to use mod_rewrite and a RewriteCond
directive that checks against the QUERY_STRING
server variable. (The RewriteRule
pattern only matches against the URL-path, which notably excludes the query string.)
For example, to redirect the specific URL /directory/in.php?url_id=28930
in the root .htaccess
file, you would do something like the following:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^url_id=28930$
RewriteRule ^directory/in\.php$ / [QSD,R=302,L]
The QSD
flag (Query String Discard - Apache 2.4+ only) removes the query string from the redirected URL. If you are on Apache 2.2 then you will need to append a ?
to the end of the substitution string instead, ie. /?
, in order to effectively remove the query string from the redirected URL. Otherwise, by default, the query string from the request is passed through to the target URL (ie. susbstitution argument).
Note also, this is a temporary 302 redirect. If this is intended to be permanent then change the 302
to 301
only when you are sure it's working OK (to avoid any caching issues).
To make this more general and redirect any URL that contains a numeric url_id
parameter value, then change the RewriteCond
directive to something like:
RewriteCond %{QUERY_STRING} ^url_id=\d+$
Aside: From an SEO point of view, mass redirects like this to the homepage are likely to be seen as soft-404s by Google and other search engines.