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

[eluser]harmstra[/eluser]
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

Code:
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?
#12

[eluser]Jelmer[/eluser]
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:
Code:
- http_docs (root)
| - subdomain
| | - .htaccess
| | - index.php
| - .htaccess

The .htaccess in the http_docs has the rewriting for the subdomain:
Code:
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:
Code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1[QSA]
#13

[eluser]harmstra[/eluser]
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.
#14

[eluser]Jelmer[/eluser]
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/
#15

[eluser]harmstra[/eluser]
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.
#16

[eluser]Jelmer[/eluser]
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:
Code:
$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):
Code:
if (SUBDOMAIN == '' OR SUBDOMAIN == 'www')
    $route['default_controller'] = "site";
else
    $route['default_controller'] = SUBDOMAIN;




Theme © iAndrew 2016 - Forum software by © MyBB