CodeIgniter Forums
Using .htaccess for dynamic subdomains - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using .htaccess for dynamic subdomains (/showthread.php?tid=13492)

Pages: 1 2


Using .htaccess for dynamic subdomains - El Forum - 11-24-2008

[eluser]psdtocode[/eluser]
Hello,

I'm curious how I would setup my server to use dynamic subdomains. I have full access to http.conf, and the entire server. I want to acheive the effect of WordPress MU, if you know how that works.

The site I'm creating allows people to create blog-like sites under my parent domain, so it would be http://sample.mydomain.com/. The domain would then route to a URL such as:
Code:
http://mydomain.com/site_manager/index.php/site/view/sample

I've gotten it to be http://mydomain.com/site/view/sample (couldn't get rid of the site/view/ part) for basic testing, however I am wanting to reroute the URL's to how they would be on the live site.

Examples:
Code:
http://sample.mydomain.com/
shows what is on:
Code:
http://mydomain.com/site_manager/index.php/site/view/sample


Code:
http://sample.mydomain.com/post/123
shows what is on:
Code:
http://mydomain.com/site_manager/index.php/site/view/sample/post/123

Horrible URL structure in my opinion, but I don't know how else I could structure it.

Thanks,
Andrew


Using .htaccess for dynamic subdomains - El Forum - 11-24-2008

[eluser]DPrevite[/eluser]
In the past I've put this in my config file:
Code:
// May not be the best place to put this, but...
$URL = explode('.', $_SERVER['HTTP_HOST']);
$config['subdomain'] = $URL[0];
unset($URL);

$config['base_url'] = 'http://' . $config['subdomain'] . '.yourdomain.com';

And then the rest of the code uses the subdomain name in the where clause when it does select statements, etc using:
Code:
$this->config->item('subdomain');



Using .htaccess for dynamic subdomains - El Forum - 11-24-2008

[eluser]Thorpe Obazee[/eluser]
may be you should also see if the user did use a subdomain? I haven't actually tried your solution but I might try it for my pet project.


Using .htaccess for dynamic subdomains - El Forum - 11-24-2008

[eluser]DPrevite[/eluser]
I had a .htaccess file that forced the www to show up, so if you went to http://example.com it would redirect to http://www.example.com and then the subdomain would be www.

If you wanted it to work without you could just add a check to see if there is a subdomain or not and then set the basedir differently.


Using .htaccess for dynamic subdomains - El Forum - 11-25-2008

[eluser]uptime[/eluser]
[quote author="DPrevite" date="1227607306"]I had a .htaccess file that forced the www to show up, so if you went to http://example.com it would redirect to http://www.example.com and then the subdomain would be www.

If you wanted it to work without you could just add a check to see if there is a subdomain or not and then set the basedir differently.[/quote]

That would do:

Code:
<IfModule mod_rewrite.c>

## Force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

</IfModule>



Using .htaccess for dynamic subdomains - El Forum - 11-26-2008

[eluser]Phil Sturgeon[/eluser]
Something like the following would do both.

If it doesnt start with www. and it has a subdomain, send it to whatever URL you like. Otherwise, make sure it has www. at the start and send it to the main site.

Code:
<IfModule mod_rewrite.c>

# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [R=301,L]

## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

</IfModule>



Using .htaccess for dynamic subdomains - El Forum - 11-26-2008

[eluser]Phil Sturgeon[/eluser]
Afterthought, if you have mod_proxy enabled in your apache httpd.conf you can add the P flag to those rules to keep it using the subdomains instead of a 301 redirect. Try this:

Code:
<IfModule mod_rewrite.c>

<IfModule mod_proxy.c>
  # Redirect to user blog (with any trailing path)
  RewriteCond %{HTTP_HOST} !^www. [NC]
  RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [P,L]
</IfModule>

<IfModule !mod_proxy.c>
  # Redirect to user blog (with any trailing path)
  RewriteCond %{HTTP_HOST} !^www. [NC]
  RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [R=301,L]
</IfModule>

## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This will cause unknown and untested issues with the base_url. Can someone try this out and let me know what happens? No internet at home... STILL!


Using .htaccess for dynamic subdomains - El Forum - 11-26-2008

[eluser]uptime[/eluser]
thepyromaniac:

Why don't you check it at home?

Local Apache + hosts file... :-)


Using .htaccess for dynamic subdomains - El Forum - 11-27-2008

[eluser]Phil Sturgeon[/eluser]
Never got round to setting up mod_redirect on my local setup. Tried once a few months back, then decided iI was busy enough with paid work. REALLY need to have another crack at that one >.<

Its normally easy enough, but for some reason I decided to try and slap the baked-in Leopard Apache setup into shape, instead of just installing MAMP. Why oh why... Confusedhut:

Anyway, does this work for you?


Using .htaccess for dynamic subdomains - El Forum - 01-26-2009

[eluser]_sty_[/eluser]
Code:
<IfModule mod_rewrite.c>

# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [R=301,L]

## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

</IfModule>

Hi guys could anybody assist how to combine these solution specified above with the default CI htaccess for excluding index.php from url

Code:
RewriteCond $1 !^(index\.php|static|js|images|robots\.txt)
RewriteCond %{REQUEST_URI} !\.(css│js│jpg│jpeg│gif)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]

I.e. I mean how to collect it in a one file?

Thanks beforehand!