Undefined-Variables
Undefined-Variables

Reputation: 55

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

Upvotes: 1

Views: 65

Answers (2)

Undefined-Variables
Undefined-Variables

Reputation: 55

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

Upvotes: 2

moys
moys

Reputation: 8033

The code below will give the 2 string you are looking for.

(^https:.*?com|\/[\w+-]+$)

Upvotes: 1

Related Questions