Custom 301 Redirects using Regex

I’ve done some research on this and cannot find an good solution. I come from an ASP.net background and am moving our site to WordPress.

My old portfolio link format is as follows

Read More
/custom-website-design/airline-aviation/5

The same link on the new website will be

/custom-website-design/airline-aviation

Note the last “/5” is not there. On my old site I used this to look up the category on the back end.

Now my question is, in wordpress, using either a plugin or a custom .htaccess file, can I have the site look for

/custom-website-design/(ANYTHING)/(NUMERIC) 

and then have it redirect to

/custom-website-design/(ANYTHING)

Thanks again!

Related posts

Leave a Reply

1 comment

  1. It can be done using WordPress rewrite module but i’m not an expert.
    Anyway, you can also do it using .htaccess and mod_rewrite.

    You can put this code in your htaccess (which has to be in root folder)

    RewriteEngine On
    RewriteRule ^custom-website-design/([^/]+)/[0-9]+$ /custom-website-design/$1 [R=301,L]
    

    Note: since you’re using WordPress, you have to put this rule before WordPress’ main rule.
    Should look like this finally

    RewriteEngine On
    
    RewriteRule ^(custom-website-design/[^/]+)/[0-9]*$ /$1 [R=301,L]
    
    # WordPress
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /index.php [L]