TDM
TDM

Reputation: 1

htaccess 301 redirect removing html and number

I have a tricky redirect and hoping you or someone can help me.

The 301 redirect I need must remove both numbers and ".html", so all the URLs with a number at the end and html.

Example:

Original URL: http://www.domain.com/articles/sampe/2015-05-14/post-name/11234566.html

New URL: http://www.domain.com/articles/sampe/2015-05-14/post-name/

So if the URL has a number at the end and .html, it must redirect.

Thank you in advance!

Upvotes: 0

Views: 310

Answers (1)

andrew
andrew

Reputation: 2098

something like this perhaps

RewriteRule ^articles/sampe/([^/]+)/post-name/([0-9]+).html$ http://www.domain.com/articles/sampe/$1/post-name/ [R=301,L]

This will 301 redirect all url patterns like /articles/sampe/XXX/post-name/YYY.html to http://www.domain.com/articles/sampe/XXX/post-name/

Edit
Expanding the above rule to work on multiple url patterns containing post-name in the url:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/post-name/([0-9]+).html$ http://www.domain.com/$1/$2/$3/post-name/ [R=301,L]

This will take all urls in your site like : /*/*/*/post-name/*.htm and redirect them to /*/*/*/post-name/

The new redirect will work on all urls like these for example:
/articles/somethingelse/2015-05-14/post-name/11234566.html
/posts/category/2015-06-10/post-name/1589.html
/demo/path/folder/post-name/999999.html

Anything that looks like: folder/folder/folder/post-name/number.html

Upvotes: 0

Related Questions