virtual subdomain: one subdomain per user - php

On my website i'm using virtual host so my users can have virtual domain like 'user1.mydomain.com', 'user2.mydomain.com',...
The problem is that on virtual domains like 'user1.domain.com' the index page is always the same as on my index page 'http://mydomain.com'.
What i want to do is to have two different index pages for the domain and for subdomains. My question, how to have subdomains redirected to 'index2.php' (for example) and still have the subdomains to look like 'user1.mydomain.com'?

Ideally, you would set up completely separate document directories for each subdomain. vhost in apache is the way to go. If you however want to do it your way (subdomains redirecting to individual files), then it's a bit more work, but is still doable. First, define the mod_vhost with wildcard:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.mydomain.com
ServerAlias *.mydomain.com
...
</VirtualHost>
Then inside this VirtualHost setup rewrite rules using mod_rewrite:
<Location "/">
RewriteCond %{HTTP_HOST} ^user1.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index2.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index2.php [R=301,L]
RewriteCond %{HTTP_HOST} ^user2.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index3.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index3.php [R=301,L]
...
</Location>
Note however that this will only work properly for / and /index.php requests to subdomains. You are much better off setting up separate document root directories for each subdomain if you intend to do anything more than this.

Maybe you are interested in this.
http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html#virtualdocumentroot
This allow, in Apache server, to automatically use a document root based in the subdomain

I would use php to check the url.
If it is a subdomain include index2.php else include index1.php
Without knowing the servers you are running on it is the only method I could think of.

Related

htaccess rewrite for wildcard subdomains

few hours later, i need help. I trie to rewrite the url for using wildcard subdomains.
My domains will be *.domain.com - for example mytest.domain.com
For every subdomain is a subfolder unter www.domain.com/sdom/content/mytest
I've setup the apache/nginx server. For testing i upload a index.html inside the "content" folder.
If i type mytest.comain.com this index.html will display. So now i upload the .htaccess file inside this folder to route from here to the destinations (in this example to the "mytest" folder).
I trie a lot of thiks, at least this
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule (.*) /httpdocs/domain/sdom/content/$1/index.html
i call mytest.domain.com but it wont go to the subfolder, it go to the maindomain.
If i delete the htaccess file, the index.html of the "content" folder will display.
After rewriting the url in the adressfield of the browser shoul display mytest.domain.com - not the new path to the subfolder etc.
What i have to do, that is works ?
UPDATE
subdomains route to -> mainfolder
subdomains should route by htaccess to folder that has the same name of the subdomain
So i trie htacess
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld$
RewriteRule ^$ /subdomainfolder/index.html
With .htaccess files you can't. Here mytest.domain.com and www.domain.com are different hostnames. So inevitably an external redirect will happen.
If you want different domains run by one physical server you should use virtual hosts:
<VirtualHost *:80> # Change this to *:443 if you use SSL/TLS
ServerName mytest.domain.com # Your domain name
DocumentRoot $SRV_ROOT/httpdocs/domain/sdom/content/mytest/ # assuming $SRV_ROOT is set to root of your server
<Directory "$SRV_ROOT/httpdocs/domain/sdom/content/mytest/">
Require all granted # on Apache 2.4
# Below are instructions for Apache 2.2
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
You'll need to restart your server each time you add a subdomain, as well as having a DNS record or local record in /etc/hosts file if you test locally.

.htaccess rewrite [virtual] subdomain to [virtual] folder

I have a site with virtual pages where any request is rewrote to the index page in this way:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
I.e, http://www.example.com/job/edit is rewrote as http://www.example.com/index.php/job/edit where I parse it. That works great.
Now, I need to process every virtual subdomain in order that i.e. http://user1.example.com/job/edit becomes http://www.example.com/index.php/user1/job/edit.
I added in CPanel a wildcard subdomain (*.example.com), so every subdomain is pointed to the domain folder (without it I got 'Server not found' on every subdomain).
I tried every example that I found here, but I can't get them to work. If I print out $_SERVER['REQUEST_URI'] on the index page, I allways get the last part, but not the subdomain (i.e. /job/edit).
Is there any way of doing this?
Thanks in advance.
Edit: I already have a workaround for doing it. The $_SERVER['HTTP_HOST'] var contains the whole domain, i.e. user1.example.com. I can parse it in PHP and extract the subdomain, but I think it should be a more elegant way of doing it.
I have Done Same Like this.
Create a Folder called "user"
Create A index.php to handle The calls To user functions
Add A Virtual host in the Httpd.conf That Route The
*.example.com to the Path /public_html/user
<VirtualHost 0.0.0.0>
ServerAlias *.example.com
ServerAdmin webmaster#yourdomain.com
DocumentRoot /home/yourdoma/public_html/user
ServerName yourdomain.com
User yourdoma
Group yourdoma
BytesLog /usr/local/apache/domlogs/yourdomain.com-bytes_log
CustomLog /usr/local/apache/domlogs/yourdomain.com combined
ScriptAlias /cgi-bin/ /home/yourdoma/public_html/joe/cgi-bin/
</VirtualHost>
Now all Call will route to user Folder.
Add a Htaccess file in user folder to rewrite the base path

How to create Multiple cakephp setup in single domain and using htaccess changing Url to main domain?

i have a domain eg. example.com
and it contains two folder
/cakeapp1
/cakeapp2
if i use individualy then it is working fine.
eg. http://example.com/cakeapp1 or http://example.com/cakeapp2
But i want these to point on the main domain some links are from first sub-directory and other are from second sub-directory.
Basically i want to do the following.
If i open http://example.com then it point to http://example.com/cakeapp1
if i open http://example.com/file then it point to http://example.com/cakeapp2/file
and
if i open http://example.com/random then it point to http://example.com/cakeapp1/random
Please suggest me the way that how i can do the above task.
There is problem with the htaccess also. if i use the htaccess in root and use rewrite then the both subdirectory's htaccess conflict.
Thanks for help!
In the httpd-vhosts conf file add a virtual host entry. Try below (i havent tested it but it might give you an idea)
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/file
RewriteRule ^/(.*) http://example.com/cakeapp2/file [R=301,L]
RewriteRule ^(.*) http://example.com/cakeapp1$1 [R=301,L]
</VirtualHost>

Redirect from subdomain to primary domain without rewriting URL

I've been searching similar solutions but none of them seems working on my server.
What I'm trying to do is set redirect from yyy.zzz.com (subdomain) to zzz.com (primary domain) without rewriting url. So both yyy.zzz.com and zzz.com actually pointing at same directory and same files in it.
Currently I have this .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^yyy\.zzz\.com$ [NC]
RewriteRule ^(.*)$ http://zzz.com [L,NC]
Which, of course, just redirects straight on, and url actually changes.
Just for information, I want to set such subdomain url for CMS, so if user wants to enter CMS he does it from subdomain, while actually only $_SERVER['HTTP_HOST'] is changing.
Any help would be appreciated.
You should set up a ServerAlias in your apache config. Setting yyy.zzz.com as an alias for zzz.com
<VirtualHost *>
ServerName zzz.com
ServerAlias yyy.zzz.com
# ...
</VirtualHost>
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias

Using mod_rewrite for subdomains

I wan't people to be able to register their own sections within my site, and have those sections be their own subdomain of my site. so someone can register 'test' and have 'test.example.com' refer to their site which would be at say /site.php?id=1 So how would i go about writing a mod_rewrite rule for this?
mod_rewrite might not be the best tool for this. RewriteRules are great for mapping a file to something else, but it was not really meant for domain matching.
The best you can do using mod_rewrite is the following:
RewriteEngine On
# Skip www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^/(.*)$ site.php?domain=%1&file=$1 [L]
Which is not really useful. What I would recommend to do instead if programmatically check the value of HTTP_HOST as such:
function get_subdomain() {
if(preg_match('/^([^.]+)\.domain\.com/i', $_SERVER['HTTP_HOST'], $matches))
return $matches[1];
}
You can configure apache to claim the "special" domains in the apache configuration (e.g. www.mydomain.com, etc), in your virtual host containers and have the others default to your first virtual host. You can then rewrite the base url to a dispatch application that inspects the URL and sends the user off to their correct area.
From Apache 2.0 docs:
Almost any Apache directive may go
into a VirtualHost container. The
first VirtualHost section is used for
requests without a known server name.
Put the code to handle *.mydomain.com in the first virtual host:
<VirtualHost *:80>
Servername custom.mydomain.com
DocumentRoot /var/www/html/custom
RewriteEngine on
RewriteRule ^/$ /var/www/cgi-bin/dispatch.cgi [R]
# more configs here
</VirtualHost>
<VirtualHost *:80>
Servername www.mydomain.com
DocumentRoot /var/www/html/homepage
# more configs here
</VirtualHost>
In dispatch.cgi, inspect the calling url to determine where the user should go. Make sure you set up your DNS to accept all subdomains.
Hope that helps!
Actually, we've been using this technique without much problem for the past year. Our mod_rewrite config looks like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydomain.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteCond %{REQUEST_URI} !admin
RewriteRule (.*) /admin/ [R=301,L]
RewriteRule ^admin(.*)$ hosted/admin/$1 [QSA,L]
What happens, is that users are directed to theiraccount.mydomain.com/admin.
We then use PHP to identify the account portion of the domain name. Using something like $_SERVER['SERVER_NAME'] or some method of getting the domain name and parsing it works very well.
Keep these configs in the httpd.conf though, this will become a cpu problem in higher traffic sites if it's kept as .htaccess.

Categories