remove trailing number from url via .htaccess

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

Read More
# 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

Related posts

Leave a Reply

2 comments

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