Looking to redirect all URLs which look similar to this:




Basically removing the following from the URL:


They are parts of an old system.

1 Answer 1

RedirectMatch 301 ^/discussion/topix/(.*)/posts/.*$ /discussion/topix/$1/

(.*) is a capturing group which matches anything preceding ‘posts’.
$1 outputs the capturing group as the destination for the redirect.

  • Thats perfect thank you, although it leaves the part of the URL starting /rate? Is there anyway of stripping that last part of the URL too ?
    – Randomer11
    Oct 11, 2017 at 8:35
  • Actually, Unfortunately that conflicts with another part of the site which has a URL such as example.com/discussion/posts/1059959 which acts differently. Is there a way of narrowing it down so it only checks for /posts/ after the /topix/ directory
    – Randomer11
    Oct 11, 2017 at 8:41
  • @Randomer See edit, it now only matches if the path starts with /discussion/topix/
    – grg
    Oct 11, 2017 at 8:45
  • Perfect thank you very much Grgarside I have marked the answer as correct :)
    – Randomer11
    Oct 11, 2017 at 8:49
  • 2
    There's no need to escape the slashes in the source URL regex. However, this won't remove the query string on the original request (you'll be left with ?rating=13..... appended to the end of the target URL). To remove the query string "cleanly" you'd need to use a mod_rewrite RewriteRule directive.
    – MrWhite
    Oct 11, 2017 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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