.htaccess Rule Slight Help Needed

I have spent hours trying to get this rule right and I am really struggling. I have got so close but I can not get it right. Any help is more than welcome... Thank you #

Trying to remove unwanted segments in the URL. From this:

To this:

The segments are random in name and in quantity, this is the rules I have so far

RewriteEngine on

RewriteCond %{REQUEST_URI} (\^|~|`|<|>|,|%|\\|\{|\}|\[|\]|\|)/ [NC]

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

It almost works but I need the last segment to be left, this rule gives a new URL of

The new url is Test are stopped, a redirect will be made with status code 301

What is needed to leave the final segment?

I am getting closer

RewriteCond %{REQUEST_URI} (^https:.*?com|\/[\w+-]+$) ~gm [NC]

RewriteRule ^https:.*?com|\/(.*)[\w+-]+$(.*) $2 [R=301,L]

Gives me

So selecting the wrong part of URL but getting closer!



This is the correct answer and I wish I could take the credit for it but I can't

RewriteEngine On

RewriteRule ^index\.php/module/action/param1/.*/(.*+)$ /$1 [L,R=301]

This Is the work of a genus, I asked him for advice after coming across a very nice tool he has created. RewriteRule_Generator