How to implement dynamic subdomains in codeigniter with .htaccess?

How to implement dynamic subdomains in codeigniter using .htaccess ?

+8
subdomain .htaccess mod-rewrite codeigniter-2 wildcard-subdomain
source share
3 answers

Make sure subdomains are enabled on your site. When you log in to test.yoursite.com, it should send you to the welcome page of your site. If instead it gives a DNS lookup error, then this means that subdomains are not enabled on your site.

To enable subdomains on your site, add the *.yoursite.com entry in the DNS zone entry.

Paste the following code into your .htaccess file and replace yoursite accordingly.

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / #codeigniter specific rule RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L] #codeigniter specific rule RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L] #this rule removes www from the URL if its used RewriteCond %{HTTP_HOST} ^www. RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L] #the auth system URIs which don't have subdomains RewriteCond %{HTTP_HOST} ^yoursite. RewriteRule ^(signup|signin|forgot_password)/?$ index.php?/auth/$1 [L] #this rule handles the subdomains RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com RewriteRule ^(.*)$ index.php?/public_site/%1/$1 [L] </IfModule> <IfModule !mod_rewrite.c> ErrorDocument 404 /index.php </IfModule> 

Add additional rules, if necessary, to handle the system or auth subdomains. I know what I did for my site to get a pretty URI.

Important Note:

I found that by default, subdomain URIs work very well with codeigniter without any of the above rules. You can access all your sites using test.yoursite.com/# URI instead of www.yoursite.com/# . But URIs are not very good, and more than one way to access the URI would be available.

So, if you use the rules above, this will give you nice URIs and, more importantly, give you unique URIs . Therefore, if you use the above rules, you will receive only one URI for the registration page, and this is http://yoursite.com/signup .

0
source share

this code works on my site, you can move your site to another domain or folder (no matter what you want).

 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond $1 !^(index\.php|application/assets|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] </IfModule> 
0
source share

This rule handles subdomains.

 RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com. should probably also include the hypen -. RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yoursite.com. 
0
source share

All Articles