1

I have a very annoying redirect I cannot figure out.

I have many hundreds of page like the below:

example.com/directory/in.php?url_id=28930

I want to redirect all e.g. /directory/in.php?url_id=28930

The redirects will all go to the homepage.

What redirect rule could you suggest?

I have already attempted many redirect rules but all fail for some reason.

  • 1
    "I have already attempted many" - it would be good to show what you have tried (at least your best attempt), as it might just be a simple mistake. – DocRoot Jun 6 '18 at 17:36
1

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.

  • For the one for Apache 2.2, would this be: RewriteRule ^directory/in\.php$ /? [QSD,R=302,L] -- when I put your above one in I get domain.com/?url_id=28930 so the url_id is retained although it does redirect to the homepage. I also get what you mean about this being seen as a soft 404 but better to have it in place than not I feel! – PaulC Jun 6 '18 at 17:49
  • Almost - you need to remove the QSD flag if you are on Apache 2.2. ie. RewriteRule ^directory/in\.php$ /? [R=302,L]. Make sure you clear your browser cache. The ? on the end of the substitution OR the QSD flag specifically removes the ?url_id=28930 (query string) part, so if you are still seeing the query string on the redirected URL then either you are seeing a cached response or something else is redirecting it. – DocRoot Jun 6 '18 at 17:52
  • How did you get on with this? If this answered your question then please mark it as accepted (click the checkmark on the left, below the voting arrows) to remove it from the unanswered question queue. Once you have 15+ rep you can also upvote answers you find useful. Thanks, much appreciated. :) – DocRoot Jun 10 '18 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.