raiwa
raiwa

Reputation: 33

.htaccess rewrite rule to strip dash plus numbers in the middle of an URL

I'm trying to find a rewrite rule to strip dash followed by any number in the middle of an URL (always only before slash). Example: http://my.site.com/apples-256/golden-delicious/ should be: http://my.site.com/apples/golden-delicious/

I tried this without success:

RewriteRule ^(.*)(-[0-9]+\/)(.*)$ $1/$2 [L]

Upvotes: 1

Views: 119

Answers (1)

raiwa
raiwa

Reputation: 33

It seems I found the rule:

# Rewrite old URLs with category IDs in the middle
RewriteRule ^(.*)(-[0-9]+)\/(.*) $1/$3 [R=301,L,R]

at least it works for me.

Now I'm using these 2 rules to strip the number also at the end of an URL:

# Rewrite old URLs with category IDs at the end
RewriteCond %{REQUEST_URI} ^(.*)(-[0-9]+)$
RewriteRule .* %1 [R=301,L]

# Rewrite old URLs with category IDs in the middle
RewriteRule ^(.*)(-[0-9]+)\/(.*) $1/$3 [R=301,L,R]

Upvotes: 1

Related Questions