I'm trying to create a .htaccess file to redirect my old domain to my new domain while ensuring that all URLs match for SEO purposes (so nothing results in a 404 error). For the most part, I have it figured out, but I am stuck on a RewriteRule for my products.
Let's say my old product URLs look like this:
https://www.oldsite.com/webshop/category/detail/237/productname.html
My new product URLs look like this:
https://newsite.com/shop/category/productname/
There are several different product categories, and the number represents the product ID, which differs per product.
I've tried to fix it with:
# Rewrite all product links
RewriteCond %{REQUEST_URI} ^/webshop/
RewriteRule ^webshop/(.*)$/detail/(.*)$/(.*)$ https://newsite.com/shop/$1/$3/ [R=301,NC,L]
But I can't get the wildcards to work properly. I want to pass through the product category and the product name. The RewriteCond
works as intended, but the RewriteRule
doesn't match and, therefore, doesn't execute.
I have been trying to find a solution for a few days now, extensively googling and tinkering with the code. I've made sure to check each alteration as well—for example, using htaccess.madewithlove.com. I've also read through the Apache documentation (which I didn't fully grasp) and numerous tutorials. I am stuck on how multiple wildcards in a single URL work.
Here are all my current RewriteRules:
RewriteEngine On
# Rewrite all product links
RewriteCond %{REQUEST_URI} ^/webshop/
RewriteRule ^webshop/(.*)$/detail/(.*)$/(.*)$ https://newsite.com/shop/$1/$3/ [R=301,NC,L]
# Rewrite the product category links
RewriteRule ^webshop/(.*)$ https://newsite.com/(.*)$1/ [R=301,NC,L]
RewriteRule ^contact.html https://newsite.com/contact-us/ [R=301,NC,L]
RewriteRule ^about-us.html https://newsite.com/about/ [R=301,NC,L]
RewriteRule ^sale.html https://newsite.com/sale/ [R=301,NC,L]
RewriteRule ^terms-and-conditions.html https://newsite.com/terms-and-conditions/ [R=301,NC,L]
RewriteRule ^customs-and-tax-information.html https://newsite.com/customs-and-tax-information/ [R=301,NC,L]
RewriteRule ^delivery-times-and-shipping-costs.html https://newsite.com/shipping/ [R=301,NC,L]
# For all remaining pages
RewriteRule ^(.*) https://newsite.com [R=301,NC,L]
Thank you for your time and effort! Any help is most welcome.
This question was answered by C3roe
in the comments. I was incorrectly using the $
behind every capture group. C3roe
also helped with the .html extension, an issue I had not realised yet I was having.
My RewriteRule should have been:
RewriteRule ^webshop/(.*)/detail/(.*)/(.*)\.html$ https://newsite.com/shop/$1/$3/ [R=301,NC,L]
Thanks!
If I could have selected the comment as the answer, I would have done so.