Dave Hilditch
Dave Hilditch

Reputation: 5439

.htaccess urlrewrite remove number

I need to write a regular expression to remove a large number from the end of the URL.

Here are some examples:

/uk/futsal-cosmos-10-panel-match-football-white-purple-24100787502654/
/uk/escape-t400-individual-rubber-dumbbells-up-to-50kg-2289101502719397/
/uk/golds-gym-standard-hammertone-plate-25-4mm-1-25kg-x-12-1736702088365877/

So basically I need the final numbers removed including the dash.

In the third example, this should become:

/uk/golds-gym-standard-hammertone-plate-25-4mm-1-25kg-x-12/

i.e. the 12 should remain.

Upvotes: 0

Views: 165

Answers (1)

mabarroso
mabarroso

Reputation: 659

If last number always has been preceded by -, try this

<IfModule mod_rewrite.c>
   RewriteEngine on

   RewriteRule ^(.*)-[0-9]+/$ /$1/ [L,QSA]
</IfModule>

Replace [L,QSA] by [R=301,L] if you want a 301 redirection

For 10 digits after the dash

RewriteRule ^(.*)-[0-9]{10}/$ /$1/ [L,QSA]

Upvotes: 1

Related Questions