0

I tried to do this myself, but I am a newbie with regex.

I have this URL:

http://[DOMAIN]/[category]/27466-some-article-is-here

Which should redirect to this URL:

http://[DOMAIN]/[category]/some-article-is-here

I simply want to remove the ID from the URL. The category could be anything, which I want to keep.

How do I achieve this?

UPDATE: I adjusted the Redirect from @taduuda to this:

RewriteRule ^(.*)\/[\d]*\-(.*)$ $1/$

This looks good with a testing tool, but this still doesn't work on my wordpress site.

My htaccess:

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

# BEGIN - Custom Redirects
RedirectMatch 301 /heute(.*) /$1
RewriteRule ^(.*)\/[\d]*\-(.*)$ $1/$2
# END - Custom Redirects

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

</IfModule>
# END WordPress
NILL
  • 3

1 Answers1

0

Not sure if this will work with every URL you have, but this whould give you a base to work off of. Note that this sepcifically checks for 5 digit IDs. If you want this to be more flexible, simply change {5} to a different quatifier like *. Also note that this might match URLs you don't want to redirect.

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Your rule:

RewriteEngine on
RedirectMatch 301 ^(.*)\/[\d]{5}\-(.*)$ $1/$2

Or a more precise variant to avoid unwanted redirects:

RewriteEngine on
RedirectMatch 301 ^(.*example\.com\/.+)(?:\/[\d]{5})\-(.*)$ $1/$2

Since you mentioned that you're new to regex, here are some tools that will help you get started:

Online Regex building/testing tool: regex101.com

Online .htaccess rewrite testing tool: htaccess.madewithlove.be

cetteup
  • 166