.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!

Leave a Reply

Your email address will not be published. Required fields are marked *