# Mod\_Rewrites For Subdomains?

**URL:** <https://forums.interworx.com/t/mod-rewrites-for-subdomains/14067>\
**Category:** General Discussion\
**Tags:** siteworx, api, subdomains, mod\_rewrite\
**Created:** [September 10, 2016, 7:28am UTC](https://forums.interworx.com/t/mod-rewrites-for-subdomains/14067 "2016-09-10T07:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Synthetisoft](https://forums.interworx.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Synthetisoft](https://forums.interworx.com/u/Synthetisoft)\
**Post date:** [September 10, 2016, 7:28am UTC](https://forums.interworx.com/t/mod-rewrites-for-subdomains/14067/1 "2016-09-10T07:28:33Z")

</div>

Here’s my objective. I need to use the Siteworx API to create subdomains for users. Once I do that they will have a subdomain and folder combo like: [user1.mydomain.com](http://user1.mydomain.com) | /home/mydomain/html/user1

I also have a mod\_rewrite rule that takes the data from [mydomain.com/data](http://mydomain.com/data) and passes it like [mydomain.com/index.php?input=data](http://mydomain.com/index.php?input=data) just like how pastebin and other sites have URLs like [pastebin.com/fHu9Ksd](http://pastebin.com/fHu9Ksd). Here’s the .htaccess…

RewriteEngine on  
RewriteRule ^([^/.]+)/?$ /?input=$1 [L]

But will this rewrite rule work for people who visit [user1.mydomain.com](http://user1.mydomain.com) or will it be bypassed? I’m about to run a test and I’m expecting it to not work. The hope though is that when someone visits [user1.mydomain.com](http://user1.mydomain.com) it will load [mydomain.com/index.php?input=user1](http://mydomain.com/index.php?input=user1) without changing what’s in the address bad ([user1.mydomain.com](http://user1.mydomain.com)). Or maybe I should actually just, upon subdomain creation, write an index.php script to /home/mydomain/html/user1.

The end goal of all this is to allow user’s subdomains to share the scripts of the platform I’m coding. Obviously copying my entire platform into each subdomain directory is not the answer. I’ll run my test and report back. Any advice is welcome.

---

<div class="post-metadata">

**Author:** ![d2d4j](https://forums.interworx.com/user_avatar/forums.interworx.com/d2d4j/32/16_2.png) [@d2d4j](https://forums.interworx.com/u/d2d4j)\
**Post date:** [September 11, 2016, 2:07am UTC](https://forums.interworx.com/t/mod-rewrites-for-subdomains/14067/2 "2016-09-11T02:07:13Z")

</div>

Hi Synthetisoft  
Good question, and I have read it a few times, to try to fully understand your question, or rather, exactly what your trying to acheive.  
If I understood correctly, which I am sorry if I have not, but I believe your wanting [user1.example.com](http://user1.example.com) to use document root of [example.com](http://example.com)  
If so, why not use a hook to change the document root of the subdomain to [example.com](http://example.com) in the vhost file for the subdomain, or there is alias (scriptalias), which I’m sure you have seen.  
[http://httpd.apache.org/docs/2.2/urlmapping.html#outside](http://httpd.apache.org/docs/2.2/urlmapping.html#outside)  
so, if using alias, I am thinking you could add this to the Skel, or create a hook to add it.  
Ofcourse, if your htaccess works as you expect, then thats lovely and please ignore my post  
I hope that helps a little and sorry if I am wrong  
Many thanks  
John

Files Outside the DocumentRoot  
There are frequently circumstances where it is necessary to allow web access to parts of the filesystem that are not strictly underneath the DocumentRoot. Apache offers several different ways to accomplish this. On Unix systems, symbolic links can bring other parts of the filesystem under the DocumentRoot. For security reasons, Apache will follow symbolic links only if the Options setting for the relevant directory includes FollowSymLinks or SymLinksIfOwnerMatch.  
Alternatively, the Alias directive will map any part of the filesystem into the web space. For example, with

Alias /docs /var/web  
the URL [http://www.example.com/docs/dir/file.html](http://www.example.com/docs/dir/file.html) will be served from /var/web/dir/file.html. The ScriptAlias directive works the same way, with the additional effect that all content located at the target path is treated as CGI scripts.  
For situations where you require additional flexibility, you can use the AliasMatch and ScriptAliasMatch directives to do powerful regular expression based matching and substitution. For example,

ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+) /home/$1/cgi-bin/$2  
will map a request to [http://example.com/~user/cgi-bin/script.cgi](http://example.com/~user/cgi-bin/script.cgi) to the path /home/user/cgi-bin/script.cgi and will treat the resulting file as a CGI script.

User Directories  
Traditionally on Unix systems, the home directory of a particular user can be referred to as ~user/. The module mod\_userdir extends this idea to the web by allowing files under each user’s home directory to be accessed using URLs such as the following.

[http://www.example.com/~user/file.html](http://www.example.com/~user/file.html)  
For security reasons, it is inappropriate to give direct access to a user’s home directory from the web. Therefore, the UserDir directive specifies a directory underneath the user’s home directory where web files are located. Using the default setting of Userdir public\_html, the above URL maps to a file at a directory like /home/user/public\_html/file.html where /home/user/ is the user’s home directory as specified in /etc/passwd.  
There are also several other forms of the Userdir directive which you can use on systems where /etc/passwd does not contain the location of the home directory.  
Some people find the “~” symbol (which is often encoded on the web as %7e) to be awkward and prefer to use an alternate string to represent user directories. This functionality is not supported by mod\_userdir. However, if users’ home directories are structured in a regular way, then it is possible to use the AliasMatch directive to achieve the desired effect. For example, to make [http://www.example.com/upages/user/file.html](http://www.example.com/upages/user/file.html) map to /home/user/public\_html/file.html, use the following AliasMatch directive:

AliasMatch ^/upages/([a-zA-Z0-9]+)/?(.\*) /home/$1/public\_html/$2

---

<div class="post-metadata">

**Author:** ![Synthetisoft](https://forums.interworx.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Synthetisoft](https://forums.interworx.com/u/Synthetisoft)\
**Post date:** [September 12, 2016, 2:55pm UTC](https://forums.interworx.com/t/mod-rewrites-for-subdomains/14067/3 "2016-09-12T14:55:29Z")

</div>

[QUOTE=d2d4j;29049]Hi Synthetisoft  
Good question, and I have read it a few times, to try to fully understand your question, or rather, exactly what your trying to acheive.  
If I understood correctly, which I am sorry if I have not, but I believe your wanting [user1.example.com](http://user1.example.com) to use document root of [example.com](http://example.com)  
If so, why not use a hook to change the document root of the subdomain to [example.com](http://example.com) in the vhost file for the subdomain, or there is alias (scriptalias), which I’m sure you have seen.  
[http://httpd.apache.org/docs/2.2/urlmapping.html#outside](http://httpd.apache.org/docs/2.2/urlmapping.html#outside)  
so, if using alias, I am thinking you could add this to the Skel, or create a hook to add it.  
Ofcourse, if your htaccess works as you expect, then thats lovely and please ignore my post  
I hope that helps a little and sorry if I am wrong  
Many thanks  
John

Files Outside the DocumentRoot  
There are frequently circumstances where it is necessary to allow web access to parts of the filesystem that are not strictly underneath the DocumentRoot. Apache offers several different ways to accomplish this. On Unix systems, symbolic links can bring other parts of the filesystem under the DocumentRoot. For security reasons, Apache will follow symbolic links only if the Options setting for the relevant directory includes FollowSymLinks or SymLinksIfOwnerMatch.  
Alternatively, the Alias directive will map any part of the filesystem into the web space. For example, with

Alias /docs /var/web  
the URL [http://www.example.com/docs/dir/file.html](http://www.example.com/docs/dir/file.html) will be served from /var/web/dir/file.html. The ScriptAlias directive works the same way, with the additional effect that all content located at the target path is treated as CGI scripts.  
For situations where you require additional flexibility, you can use the AliasMatch and ScriptAliasMatch directives to do powerful regular expression based matching and substitution. For example,

ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+) /home/$1/cgi-bin/$2  
will map a request to [http://example.com/~user/cgi-bin/script.cgi](http://example.com/~user/cgi-bin/script.cgi) to the path /home/user/cgi-bin/script.cgi and will treat the resulting file as a CGI script.

User Directories  
Traditionally on Unix systems, the home directory of a particular user can be referred to as ~user/. The module mod\_userdir extends this idea to the web by allowing files under each user’s home directory to be accessed using URLs such as the following.

[http://www.example.com/~user/file.html](http://www.example.com/~user/file.html)  
For security reasons, it is inappropriate to give direct access to a user’s home directory from the web. Therefore, the UserDir directive specifies a directory underneath the user’s home directory where web files are located. Using the default setting of Userdir public\_html, the above URL maps to a file at a directory like /home/user/public\_html/file.html where /home/user/ is the user’s home directory as specified in /etc/passwd.  
There are also several other forms of the Userdir directive which you can use on systems where /etc/passwd does not contain the location of the home directory.  
Some people find the “~” symbol (which is often encoded on the web as %7e) to be awkward and prefer to use an alternate string to represent user directories. This functionality is not supported by mod\_userdir. However, if users’ home directories are structured in a regular way, then it is possible to use the AliasMatch directive to achieve the desired effect. For example, to make [http://www.example.com/upages/user/file.html](http://www.example.com/upages/user/file.html) map to /home/user/public\_html/file.html, use the following AliasMatch directive:

AliasMatch ^/upages/([a-zA-Z0-9]+)/?(.\*) /home/$1/public\_html/$2[/QUOTE]

I will try these things (Both what I mentioned and your ideas). So just to clarify, I’m selling my platform as a white label service to companies who will have many users and whose users may collide with other companies who use my platform’s users. The strange thing about what I’m doing is that the purposes of giving each of my site’s users a subdomain from which they can access my platform/site are 2:

1. So that when the user visits their subdomain and my site loads, it automatically associates the visit with the correct account by the subdomain they have visited. This way, their custom settings are loaded and authentication is checked only against their users. Sort of a white label thing.

2. So that they can create a cname at their domain. So let’s say their site is [example.com](http://example.com). They could create a cname at their site that points [service.example.com](http://service.example.com) to [user1.mysite.com](http://user1.mysite.com). That way, white labeling is also reflected in the URL when the customer’s users login.

It’s sort of a nested thing where I’m proving this service to companies. I’ve seen other platforms do this same thing offering subdomains you can point a cname to. Anyway, the flow goes like this… A company comes to my site and wants to use my platform. The company will have many users. Conceivably, many users from Company A using my service might also be users of Company B using my service. Also, each company has their own settings on my platform so that when their users login via their subdomain or cname, it loads the company’s styles, logo, and all manner of settings that only pertain to that company who rents that subdomain from me.

Like I said, I’ve seen this exact thing done many times. I’m just not sure exactly how they’ve handled it. You see though my need to provide subdomains (For the sake of cnames and white labeling) and also very important, that I do this all with one code base. I can’t go copying my whole platform around to every subdomain. That would be silly. I’ve also been debating with myself whether or not I should give each company their own MySQL tables for their users/settings/stats/whatever but that’s a whole different conversation.
