Categories
Development

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

https://example.com/index.php/module/action/param1/static/PFBC/js/jquery/rss/static/css/js/jquery/smoothness/templates/themes/datelove/img/icon/xml/sitemap/user/help/rss/legal/rss/comment-picture

To this:

https://example.com/comment-picture

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 https://example.com/
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

https://example.com/index.php

So selecting the wrong part of URL but getting closer!

Leave a Reply

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