1

I added this rule

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

to remove a trailing number preceded by a hyphen from an url on a WordPress site

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.*)-[0-9]+/$ /$1/ [L,QSA]
</IfModule>

But it doesn't seem to do the trick, to me the regex seems ok but I presume it conflicts with the other rules

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
Stofke
  • 2,928
  • 2
  • 21
  • 28

2 Answers2

1

You may try this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Additional 2 lines to remove the trailing -Number from the URL
RewriteCond %{REQUEST_URI}  ^/(.*)-(?:[0-9]*)?/?$    [NC]
RewriteRule .*   %1        [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php   [L]
</IfModule>

The additional 2 lines redirect permanently

http://example.com/any/number/of/folders/anything-NUMBER

To:

http://example.com/any/number/of/folders/anything

The -NUMBER combination must be the trailing string in the URL, with or without trailing slash.

The hyphen - is removed always, even when there is no number. If you want to keep it, replace

RewriteRule .* %1 [R=301,L] with

RewriteRule .* %1- [R=301,L]

Permanent redirection is used to show the substitution URL in the browser's address bar. For silent mapping, remove R=301 from [R=301,L].

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
0

RedirectMatch 301 ^(.*)-[0-9]+/?$ $1/

Brynner Ferreira
  • 1,527
  • 1
  • 21
  • 21