Is there any way to make all requests to any subdomain load the same WordPress website e.g. user1.example.com, user2.example.com and user3.example.com all load the same website, but with the links pointing to the current subdomain?
I’d like to keep mostly the same content across the different websites. The only difference is that by reading the subdomain I can offer customized content (website title, etc) specifically to that user, or add a hook to display an error message if the user doesn’t exist. At the moment the network install requires me to manually define every website, with different contents across them.
In WordPress you can easily do this with sub-directories like
example.com/user1
Sub-domain & URL Strategy
Having
username.domain.com
will prevent you in the future from having your own sub-domains likeshop.example.com
and will plague you if you wanted to usewww.example.com
or justhttp://example.com
Finally … what if some user wants to use expletives or special characters in their username <– not very good.
Traffic Load
Sub-domains are analysed (sic) by DNS servers around the world to figure out how to route traffic. If you want to use many sub-domains, this will also increase the load on your Apache web server as it tries to figure out what to do with
someusername123456789.example.com
But to do this … you’d need to look at scripts, htaccess and rewrite rules and then, this question is probably better suited for a different forum.
Sub-directories are easy along with URL parameters
Its safe to say that sub-direcotries are easy (WordPress Author pages as example) and then WordPress can analyse this and determine what to do.
You can even use URL parameters like
www.example.com/category/?user=username123456789
In summary — don’t do subdomains for usernames it can cause multiple headaches that you don’t want.
To me it sounds like this might be something better suited to a single site install vs. multi site. But it really depends on how customized things need to be for a single user.
NOTE: this answer will not include information about server setup, etc.
First off, I would define WP_HOME and WP_SITEURL in
wp-config.php
and make them unchanging. You can probably set these dynamically, but the results needs to be that they point to the main, root domain. My local WP install iswordpress.dev
, so I’ll use that throughout this answer.Example:
Next we need to set the user based on the current subdomain. This should be relatively easy: parse the HTTP host, look for a user by that username, set that user as the user for later. I’d suggest wrapping everything in a class (a singleton here).
Then we need to write something to parse
$_SERVER['HTTP_HOST']
and see if we get a valid username from it.Now that you have a username you can do all sorts of things. As an example, let’s change the blog tagline to a greeting for that user.
Assuming you use the root, naked (no www) url for your install, WordPress will send cookies to all the sudomains. So you, can check to see if a user is viewing their own subdomain and throw them back to the root, otherwise.
Finally, the last thing to consider would be that WordPress allows things in user names that won’t work with the domain name system. Like
user.one
is a valid user name. Butuser.one.yoursite.com
is two subdomains deep and not going to work.So you’ll need to hook into
pre_user_login
and sanitize things.all of the above as plugin.
There a lot of concerns that aren’t addressed in this answer. Does this scale to where you need it to scale? How will having multiple subdomains of very similar content impact search optimization? How much content gets customized? If it’s a lot, would multi-site be better suited for this task?