0

I've got a woocommerce installation that sells things. We want to offer affiliates to sell our products with their own url, ie mysite.thebasesite.com. Basically, the way that it works is that a visitor would hit the above url and I have written a plugin that extracts the "mysite" segment, does an API call to an API to get the affiliate information and then uses that information to customize some things in the header, set a cookie/session object later used to store affiliate information in the woocommerce order. All the above works when I hardcode the url segment into the plugins.

My problem is getting wordpress to maintain the subdomain in the url throughout the session. First, if I visit mysite.thebasesite.com, it immediately forwards me to thebasesite.com. Not sure why. Second, all the urls in wordpress are absolute, which I think I can get around, but still annoying. Third, all the emails from the site (particularly orders and whatnot) will have links from thebasesite.com and not mysite.thebbasesite.com. I can likely get around this too, but I'm wondering if I'm barking up the wrong tree here.

I have no aversion to using multisite but I don't think that will work because all the content (products, themes, plugins, content) is all the same for each site AND there could be thousands of affiliates and therefor thousands of subdomains. I'm starting to think that this is just something that is going to be impossible with woocommerce and wordpress.

  • There are marketplace plugins that manage all off this site and none of them use multisite. If you must have subdomains that multisite is probably the right way to go. – Tony Djukic Jul 25 '20 at 8:36
  • I'm not sure a multisite will work because I've already got over 3000 affiliate subdomains and I can't imagine having those all be multisite locations. I literally just need wordpress to keep the subdomain in the URL. That's it. I'm close but the home page is always stripping the subdomain for whatever reason. – Andy Jul 27 '20 at 18:22
0

It's as possible/impossible as you have time/budget for. It seems achievable to me, given that I'm assuming you're a for-profit organisation and this is generating you revenue somehow, but you might end up hacking some Wordpress internals at some point.

It's a broad and subjective question, which is not ideally suited to this stackexchange, but a couple of ideas below which are not 'solutions' just things worth considering.

1 - Use URL rewriting to make the subdomain a parameter passed to a single domain

Using e.g. the below mod_rewrite rules, taken from [here][1], you could perhaps solve some of your problem by getting the subdomain into a parameter you can easily work with in code.

The below htaccess (or something like it) would assume that you actually only have a single website at e.g. domain.com and a visit to affiliate123.example.com/foo/bar.php would actually appear to the server as example.com/foo/bar.php?sub=affiliate123.

If you make sure that all the URLs on your site are rendered relative to the domain (i.e. your URLs are '/foo/bar/baz.jpg' rather than 'https://www.example.com/foo/bar/baz.jpg') then the browser will consider them to be URLs in the current domain and you have a lot less stuff to worry about.

You'd then need to do some coding in order to be able to map this parameter in your URL that gives you the subdomain to lookup theme components, etc.

This probably has various issues that you'd have to figure out, like what happens if a plugin messes with your sub URL parameter or some other URL construction corner cases.

RewriteEngine on
RewriteCond %{QUERY_STRING} !^sub=
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+)\.domain\.com$
RewriteRule ^(/?)$ $1?sub=%1

2 - What do you do when the affiliate wants to put some content on their page

Occurs to me that it's quite possible that an affiliate or someone in your organisation might have the requirement where they want some content specific to one of the affiliates to be posted only on that affiliate's page. I'd suggest you either allow for that now or make it clear that it's going to be impossible. If this is a possible future requirement, it makes Multisite a potentially much better option because of the ability to allow each affiliate to manage their own content.

3 Management tools

Seems like you definitely need to look into management tools that are going to let you manage a large number of similar sites from e.g. a centralised 'control panel' or set of scripts that you write. There are tools with plugins out there that let you manage many sites from a single dashboard. This could meet a bunch of your requirements. (This also mentioned by @TonyDjukic while I was drafting this answer)

Hope some of that helps you figure out which way to go. [1]: https://stackoverflow.com/questions/42263265/how-to-rewrite-subdomain-and-subsubdomain-into-a-parameter

  • Thanks for the reply. First, I don't need the ?sub=affiliate123 part at all, I can map that from the domain and wordpress doesn't need to know about the affilate code from the query string because I'm hitting an API with the subdomain segment. I made sure all links were relative, wrote my own rewrite and it worked for the most part, except for the home path (domain.com) No matter what I tried with re-writes, the home page alway stripped off the subdomain. Not sure if that's a wordpress thing or maybe one of the plugins and that's frustrating. – Andy Jul 27 '20 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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