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

3
  • I'd suggest putting that rule ahead of the rule just prior to it Commented Jan 31, 2013 at 21:30
  • Is there a trailing slash on the URL like your rule specifies? Commented Jan 31, 2013 at 21:41
  • yes there is a slash at the end however there is not always a number Commented Jan 31, 2013 at 22:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Can you tell how I can remove the exact same things but only if there is more than 6 trailing numbers ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.