sanitycheck
sanitycheck

Reputation: 298

htaccess regular expression help needed

I'm not very good at regular expressions at the best of times, and right now I have a problem that's giving me a headache with a .htaccess file. Can anyone here help me write the right command?

I'm trying to RedirectMatch permanent for URLs that are patterned like the following:

http://www.mydomain.tld/topic000-my-name-slug.html

where 000 represents a 1, 2, or 3 digit number.

I want to rewrite these URLs to the following pattern:

http://www.mydomain.tld/my-name-slug

The phrase "my-name-slug" can vary in length and always separates words with hyphens. I don't want the .html on the end any more.

Can anyone help me with this?

Upvotes: 0

Views: 46

Answers (3)

rullof
rullof

Reputation: 7424

Try this:

RedirectMatch 301 topic[0-9]{1,3}-([\w-]+).html /$1

Upvotes: 1

Paprik
Paprik

Reputation: 159

This should work:

RewriteRule ^/topic[0-9]+-(.+)\.html$ /$1/ [R=301,L]

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143856

Try:

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

Or:

RedirectMatch 301 ^/topic[0-9]+-(.+)\.html$ /$1

if the original url literally starts with "topic".

Upvotes: 0

Related Questions