Dynamic subdomains with URL Rewriting


To define subdirectories in the root directory as a subdomain dynamically . By defining Apache's virtual hosting like *.domainname.tld we bind all subdomains to root directory. And we edit .htaccess file like that:

RewriteEngine on
# SUB DOMAINS
RewriteCond %{http_host} !www\.([a-z0-9-]+)\.([a-z0-9-]{2,5})$ [NC]
RewriteCond %{http_host} ^(www.)?([a-z0-9-]+)\.([a-z0-9-]+)\.([a-z0-9-]{2,5})$ [NC]
RewriteRule (.*) /clients/%2/$1 [L]
# /SUB DOMAINS

Create a .htaccess file in clients/ directory then write

RewriteEngine off

in it. We can access directories in the clients/ directory by using an url like subdomain.domain.tld now.