-1

I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)

I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.

However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions. (Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)

  • redirect function for products
  • redirect function for categories
  • redirect function for tags
  • individual redirects for the rest

There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.

Examples:

oldomain.com/product/any-product-ABC

redirect to

newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)

Then same redirect for languages

oldomain.com/es/product/any-product-ABC

redirect to

newomain.com/es/product/any-product-ABC

How do I write the above redirects?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Does this answer your question? [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – CBroe Dec 09 '22 at 11:56
  • This is modrewrite to make urls look different "pretty". I am looking to do a series of redirects from one domain to a new domain. The URLs are already "pretty". – Sascha Ehrentraut Dec 09 '22 at 20:54
  • It explains the basics of URL rewriting, and that is what you need here. – CBroe Dec 12 '22 at 07:40
  • "I say htaccess but maybe there is a server side way that is better" - So you do have access to the Apache server config? – MrWhite Dec 12 '22 at 21:18

1 Answers1

0

I say htaccess but maybe there is a server side way that is better

If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.

Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.

For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:

RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0

This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).

However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]

Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.

Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).

You should first test with 302 (temporary) redirects to avoid potential caching issues.

so I thought it better to break into into chunks/functions.

That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.

And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.

MrWhite
  • 43,179
  • 8
  • 60
  • 84