Webhosting – subdomains

  htaccess, Settings, Web hosting

On the webhosting is possible to create subdomains and for each subdomain create their own content. Just create subdirectory for your subdomain website in directory www/subdom on FTP.

For example, if we have hosting for the domain example.com and we want to create a subdomain karel.example.com, on FTP create directory www/subdom/karel and upload subdomain website files into this directory. This page will displayed on address http://karel.example.com/ (it will also work for www.karel.example.com).

Also, make sure that you have set appropriate A record in DNS which is directing domain to particular webhosting server – either a specific subdomain or for whole domain (wildcard record).

Also, read:

The different subdomains for different aliases

If you are using aliases and subdomains on hosting at the same time and create for example directory www/subdom/example, then this will create a common subdomain example for all aliases. This may be unwanted.

If you want to have different subdomains for different aliases (for example example.alias1.cz should be different than example.alias2.cz), then create the following subdirectories in www/domains, www/domains/example.alias1.czwww/domains/example.alias2.cz.

Content of nonexistent subdomains

If someone proceeds to address of the subdomain, which does not have created the appropriate directory on your site, a visitor will see content from directory www, of main domain. If this is unwanted, you can use an easy trick – move the main domain from web directory www into subdirectory www/domains/main-domain.cz – so use directory structure like as for alias. Then nonexisting subdomains would display error page because in www directory is not an index but you can place some your error page there or you can redirect this page somewhere else.

But in www directory always leave our prepared .htaccess file, otherwise, websites will not work.

Htaccess file

Subdomains are made by using rules that are in the file .htaccess on FTP. If you want to cancel using subdomains, delete or rename this file.

Following .htaccess file is created on webhosting, there is no need to modify it. To use it, just create the required directory according to the instructions above.

The rules for individual sites of aliases and subdomains in the file .htaccess are as follows:

RewriteEngine On

# domains (aliases)
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

# subdomains (with or without www at the beginning)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliases - properly redirected on missing /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomains - properly redirected on missing /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

This code solves also creating of separate sites for aliases.

Byl pro Vás tento návod užitečný?