Redirect woocomerce and my_account to subdomain

Hi,
We need to redirect woocomerce products and my_account to a subdomain, as we've separated public site and admin/WC.

How can we do that in one go?

Thanks!

  • Kasia Swiderska
    • Support nomad

    Hello 1M,

    Can you provide me with more information about your setup? Do you have now Single WordPress install on your main domain and there you have also installed a WooCommerce and now you want to move WooCommerce and products to installation on the subdomain?
    Or it is something else? Let me know

    kind regards,
    Kasia

  • D.
    • The Crimson Coder

    Hi Kasia,

    Exactly. The example.com is a single WP site installation, with WooComerce installed. The same for the subdomain.example.com

    As a matter of fact, the subdomain is a clone of the main site (in order to maintain the product ID's).

    For now, we've been manually creating individual 301 redirects (bad idea) from the main sites' WC and /my_account URL'S to the subsite. We've noticed that we keep on missing on URL's and example.com/my_account/* to subdomain.example.com/my_account/$1 redirects are not working well.

    What we want to achieve, is: an user wants to register, login, etc to /my_account (so on any page different to wp-admin), he's redirected to the subsite. When clicking on any URL that was pointing to any WC product, the user should be redirected to the same product, but on the subsite.

    Hope I wasn't confusing :slight_smile:

    Andrea

  • Patrick Freitas
    • Staff

    Hi M.

    How are you today?

    Thank you for clarity the case of use.

    I made some tests on my end using the template_redirect, but couldn't find any easy hook to create this behaviour for all links, and you would need to get the permalink and append on a 301 redirect.

    If the new website and old website products have the same ID, you can use this hook, please, create an MU Plugin, references on https://codex.wordpress.org/Must_Use_Plugins

    Paste the following code:

    <?
        add_action('template_redirect','custom_shop_page_redirect');
        function custom_shop_page_redirect(){
            if (class_exists('WooCommerce')){
                if(is_product()){
                    $id = wc_get_product()->get_id();
                    wp_redirect('https://newurl.com/product/?p='.$id,301);
                    exit();
                }
            }
            return;
        }

    Replace the https://newurl.com/ for your URL, it should create a 301 redirect for the products, however, this trick only will work if the old site and the new site have the same Product ID. I will be looking for something better, but I'm afraid you will need to extend the code to get all Woo links, like cart/account...

    Best Regards,
    Patrick Freitas

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.