How to implement dynamic subdomains in codeigniter with .htaccess?
Asked Answered
M

3

8

How can I implement dynamic subdomains in codeigniter with .htaccess?

Merely answered 16/3, 2012 at 5:27 Comment(2)
-@Merely always have been curious about this, thanks for sharing, really nice work!Cavicorn
It would be ideal if you asked this in a question and answer format, as this now appears in the unanswered list. It is perfectly acceptable to ask and answer your own question (and mark it as accepted). You won't be cheating the system and it's actively encouraged if your question and answer are likely to help other people. (Nonetheless - thank you for this)Clotilde
A
0

Make sure that subdomains are enabled on your site. When you enter test.yoursite.com it should take you to the welcome page of your site. If instead it gives DNS lookup error then it means subdomains is not enabled on your site.

To enable subdomains on your site add *.yoursite.com entry to the DNS Zone records.

Second insert the following code in your .htaccess file and replace yoursite appropriately.

<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 more rules, if necessary, for handling auth system or subdomains. I know I did for my site to get pretty URIs.

Important Note:

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

So if you use the above rules it will give you pretty URIs and, more importantly, will give you unique URIs. So if you use the above rules you will only get one URI for the signup page and that is http://yoursite.com/signup.

Affable answered 18/5, 2015 at 11:42 Comment(0)
G
0

this code is working for my site, you can move your site into your other domain or folder ( whatever 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>
Greathearted answered 27/2, 2016 at 7:23 Comment(0)
S
0

This rule handles the subdomains

RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com.  should probably also include the hypen -.  RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yoursite.com.
Supernaturalism answered 6/9, 2016 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.