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
  • Thanks for your help. Unfortunately this didn't work properly, even as I was changing it a bit. I used RewriteRule ^(.*)\/[\d]*\-(.*)$ $1/$2, which looks right with the testing tool, but doesn't work on my site. Any suggestions? – NILL Feb 26 '18 at 09:34
  • The regex does not seem to be the problem then. A few questions: Where are you trying to implement the .htaccess? Are your sure your webserver allows to change settins via .htaccess? And, since you are using Wordpress, have you tried changing the settings for the permalinks in Wordpress itself ? – cetteup Feb 26 '18 at 10:08
  • I added the redirects to the .htaccess in root. I already did another redirect, which was working. And changing the settings for permalinks is no option, because we are transfering the site from Joomla to WordPress and the Joomla-IDs don't match the WordPress-IDs – NILL Feb 26 '18 at 10:16
  • Could you add the content of your .htaccess to your question? The two redirects might be interfering with oneanother. – cetteup Feb 26 '18 at 10:18
  • My bad, you'd need to use redirect rather than rewrite. Try setting it up with my edited answer. – cetteup Feb 26 '18 at 11:55