damargon
damargon

Reputation: 41

Redirect to a different folder with the same file name

I am renovating a website and I am encountering a problem with URLs that are already indexed in search engines. These URLs have this path:

https://www.example.com/products/product-1
https://www.example.com/products/product-2...

And in the new website the path changes to:

https://www.example.com/items/product-1
https://www.example.com/items/product-2

The product-1, product-2 still retains the name but changes the folder /products with /items.

How can I do this in .htaccess so that those URLs already indexed do not give error 404 and redirect to the /items folder?

Upvotes: 1

Views: 659

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Try the following using mod_rewrite at the top of your root .htaccess file:

RewriteEngine On

RewriteRule ^products($|/.*) /items$1 [R=301,L]

(You do not need to repeat the RewriteEngine directive if it already occurs in the file.)

The 301 (permanent) redirect ensures that any SEO value of the old URL is preserved. Search engines will see the redirect and index the new URL instead.

However, please test with 302 (temporary) redirects first to avoid potential caching issues.

The above redirects:

  • /products to /items
  • /products/ to /items/
  • /products/<something> to /items/<something>

But does not redirect:

  • /products<something>

Upvotes: 3

Related Questions