How to create subdomain for each store for a multivendor ecommerce website?

Viewed 44

Im working on a multivendor ecommerce website and I want to create subdomain for each new user.

My question is how do I create dynamic subdomains like http://storename.mywebsite.com ?

For the moment my website is working this way: http://mywebsite.com/storename

I get the value of store name this way:

$URL = explode('/', $_SERVER['QUERY_STRING']);
$store = $URL[0];

I got stuck in what do add in my .htaccess and how to get the value of storename from the subdomain (storename.mywebsite.com)

Currenty my .htaccess looks like this:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

# directory browsing
Options All -Indexes

N.B

I checked some answers here and I did use a wildcard and specified A records

*.mywebsite.com IN A 127.0.0.1 =>(Server IP Address)

1 Answers

First, you should really kick off with a multi-domain security certificate and code for HTTPS. It will be much harder later on.

From my experiments, you can do this

RewriteCond %{HTTP_HOST} ^mywebsite.com$
RewriteRule ^storename1$ http://mywebsite.com/storename1?{QUERY_STRING}  [R=302,L]

But there are problems with this.

  1. You need a separate certificate for every subdomain or one multi-domain certificate if you want to use HTTPS
  2. You have to repeat this for every subdomain
  3. It does a redirect so that the user actually sees the end URL (the URL with the subfolder is what the browser will show to the user)

If you want to avoid point 3, then the redirection has to happen higher up. For instance, if your provider is using cpanel and you have access to it, then you can map each subdomain to a subfolder and the user will not see the subfolder in the URL path.

If you don't have access to such a tool, then ask your provider to map it for you. Every time you add a new subdomain, you have to ask them to map it for you.

Related