2

Looking to redirect all URLs which look similar to this:

https://www.example.com/discussion/topix/random-page-title.133760/posts/1255466/rate?rating=13&_Token=78139%2C1464617730%2Ca42dfb1db7e54b51db1879075650aaf4a3c3323a

to

https://www.example.com/discussion/topix/random-page-title.133760/

Basically removing the following from the URL:

posts/1255466/rate?rating=13&_Token=78139%2C1464617730%2Ca42dfb1db7e54b51db1879075650aaf4a3c3323a

They are parts of an old system.

1 Answer 1

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.

6
  • 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 that you have read and understand our privacy policy and code of conduct.

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