简体   繁体   中英

Sub Domain Name Creation in .htaccess

I have an application on SAAS basis where I will be adding clients from backend or the clients can even create their own panel by signing up

While signing up or adding the client , the client or I will be assigning the sub domain . So suppose I am assigning subdomain xyz to client1 . Then the access url will be

xyz.maindomain.com

Now , I want .htaccess code in such a way that if xyz.maindomain.com is called, the url called must be maindomain.com?client=xyz .

Please guide me how to do this.

Thanks in advance.

We could use mod_rewrite to achieve what you wanted. No server-side scripting such as PHP required.

Say, put this in .htaccess file on your root www directory (or any relevant Apache *.conf file if you wish)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/?client=%1/$1 [L,R=301]

This will redirect foo.example.com to http://www.example.com/?client=foo

(change example.com to your domain name)

More about mod rewrite, with examples:

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM