Welcome Guest, Not a member yet? Register   Sign In
virtual subdomain with htaccess

Hello Jelmer,
Can you explain this a little furter?

I created the rules you suggested. Then, i created the 'subdomain' directory with a .htaccess in it.
This htaccess contains the default CI rules

RewriteCond $1 !^(index\.php|images|robots\.txt|src|ext)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA]

Do i also copy the roots index.php to the subdomains dir?

Hi harmstra,

Saw your email but didn't have time to answer sooner.

The basic idea is that the rewriting of the subdomain to the directory is done from your root and that removing the index.php from the directory is done from the CI directory itself.

If we have a filesystem like this:
- http_docs (root)
| - subdomain
| | - .htaccess
| | - index.php
| - .htaccess

The .htaccess in the http_docs has the rewriting for the subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST}    ^subdomain.domain.com$
RewriteCond %{REQUEST_URI}  !^/subdomain/
RewriteRule (.*)            /subdomain/$1 [last]

And the .htaccess in the subdomain directory has the rewriting for the index.php file, for example:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1[QSA]

Hi Jelmer
I don't understand the part "and that removing the index.php from the directory is done from the CI directory itself."

In your filesystem overview, there is no index.php in the root, but there is one in /subdomain.

Can you explain this? I think the index.php in the root has to be in place, otherwise all the URLs other than /subdomain/etc/etc won't work.

I hadn't noticed that you actually wanted it to work like controller.domain.com to show www.domain.com/controller. I thought you wanted a seperate CI install on a subdomain.

There are multiple solutions for such an implementation, you could find more on this by searching for username as subdomain on the forums (that's a many times over discussed topic).

Some examples:
- http://ellislab.com/forums/viewthread/51627/
- http://andy.sinaptix.com/2007/08/22/user...deigniter/

I read both post, but these do not address the same issue as far as i can see.
Can you please reread my initial post? This describes the problem i am facing.

While they don't address your problem specificly they find a way to map subdomains to a specific function of a specific controller. You want to map subdomains to corresponding controller, which is quite similar.

If your current domain works as a wildcard every subdomain should be handled exactly like the domain (if not, look at the instructions from the 2nd url).

The point of the username as URL is that everything works from the document root which is called on every subdomain and doesn't need "physical" directories to work. Important from the second URL are the instruction about the base_url:
$config['base_url'] = "http://".$_SERVER["HTTP_HOST"];

And the first URL learns you to define a constant that contains the subdomain. After which you can change the default_route in the routes.php by testing for the contents of the constant. And you could have each subdomain handled by different controllers this way. I prefer to set the constants in the main index.php instead of a config - but that's probably not considered good practice.

For example (with constant SUBDOMAIN set):
if (SUBDOMAIN == '' OR SUBDOMAIN == 'www')
    $route['default_controller'] = "site";
    $route['default_controller'] = SUBDOMAIN;

Theme © iAndrew 2016 - Forum software by © MyBB