Nathan
Nathan

Reputation: 7855

How to remove only part of URl string using apache url rewrite?

I have thousands of URLs that look like this:

http://www.domain.com/here-is-a-product-ab00007a1.html

The number at the end of the URL (before the .html) is the SKU. The site has recently been relaunched and these numbers no longer exist in the URLs.

I would like to redirect all URLs like this to their new version which does not include the SKU, example:

http://www.domain.com/here-is-a-product.html

Any help would be greatly appreciated! Thank you.

===== EDIT =====

The actual sku format is like so:

aa00000a00

There are always 2 letters, followed by 5 numbers, followed by 1 letter, followed by 2 numbers.

The sku is always 10 characters long.

Upvotes: 0

Views: 923

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

Try putting this in the htaccess file in your document root:

RewriteEngine On
RewriteRule ^(.*)-[a-z]{2}[0-9]{5}[a-z][0-9]{2}\.html$ /$1.html [L,NC]

You can also put that in your vhost/server config if you'd rather do that, just remove the leading slash from the rule's target so that it's simply: $1.html

Upvotes: 1

Related Questions