1

I have an old site with thousands of products and some SEO ranking and I wanted to redirect URLs using .htaccess, hopefully without manually writing thousands of 301 redirect entries.

The domain stays the same but url structure is different

Old website

example.com/category/sub-category/2350-Product-Name (URL has an id and Capitalized letters)

New website

example.com/different-category/different-subcategory/product-name (URL has no id and no lowercase letters)

if I used the code below, would it be enough for a successful redirect?

# 301 Redirect URLs.
Redirect 301 /example.com/category/subcategory /example.com/different-category/different-subcategory

I found this code in another thread, which, if I understand correctly, redirects to category/subcategory that can consist of the characters a-z, A-Z, 0-9, _ (underscore) or - (hyphen), but I'm not sure how to piece this all together

RewriteRule ^([\w-]+/[\w-]+/)$
2
  • "url has no id and no lowercase letters" - presumably you mean "no uppercase letters" (or consists solely of lowercase letters)? How many category + subcategory combinations do you have? This would seem to determine how many rules you would need in .htaccess.
    – MrWhite
    Oct 13, 2022 at 21:19
  • you need to use RewriteMap .... httpd.apache.org/docs/current/rewrite/rewritemap.html
    – Wayne
    Oct 14, 2022 at 8:24

1 Answer 1

1

Redirects vs RedirectMatch vs RewriteCond & RewriteRule & RewriteMap

Redirects work for changing a url on the host, but don't know the host.

Redirect "/foo.html" "/bar.html"
Redirect "/docs/" "http://new.example.com/docs/"
# UPPERCASE to lowercase
Redirect "/category/sub-category/Product-2350/" "/new/product-2350"
Redirect "/category/sub-category/Product-2351/" "/new/product-2351"
Redirect "/category/sub-category/Product-2352/" "/new/product-2352"

RedirectMatch gives Regex almost RewriteRule but no functions, server / logic conditions.

RedirectMatch "^/(puppies|canines)/(.*)" "/dogs/$2"

or

RedirectMatch "^/CATEGORY/sub-category1/(.*)" "/new-category1/$1"
RedirectMatch "^/CATEGORY/sub-category2/(.*)" "/new-category2/$1"

RewriteMap has the to lowercase function you need, If you have many products.

#writes a lowercase version of itself.

RewriteMap lc int:tolower
RewriteRule "(.*)" "${lc:$1}" [R]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.