1

I am using MVC, specifically Codeigniter.

I have a need to create a sub domain on the service I am building. To pass a more "Mobile" friend version of the service to a user. Via m.example.com. Generally I would just add a virtual host, put everything in a folder, and so on and so forth, but since I am using MVC on this project most of the functionality lies in models and libs of which are agnostic to the view or even the controllers as far as the output they have.

So my current train of thought is that it would be a waste of resources to create a whole new folder on the machine that has the same exact stuff as the mvc currently in use has, just for the sake of using it as m.example.com. So I am wondering (cause it works this way currently with index.php) Is there a way to say I assume through htaccess that when traffic through m.example.com comes through, that I use a specific controller path, rather than a folder path? So that way just like its big brother index.php m.php (controller in this example) can handle all the core traffic and just have a bunch of functions that connect to the models, libs, etc..

In a comment I made below to someone, I said:

No, m.php is not in root, it's a controller.. I am trying to route any requests to m.example.com to the m.php controller (m.php is in the controller directory, following suit with the MVC pattern), I can access http://example.com/m/function/param/ following the MVC logic, but I would like to have it so http://m.example.com/function/param/ is equal to that of the mvc logic

Which ultimately means I am trying to come up with a logic either through .htaccess or some other means that http://example.com/m/function/param/ and http://m.example.com/function/param/ are the same exact thing, preferably without making a mirror copy of the entire CI installation and code I've built on to it, and putting it in a directory called m for example. I emphasize on this cause there seems to be some confusion.

2 Answers 2

2

You might want to take a look at this, and just alter it to use the HTTP_HOST instead of the user_agent library shown here

http://digitizor.com/2011/12/05/codeigniter-mobile-browser/

6
  • Interesting, that is one ideal on it, and I think with some better planning down the road when we rebuild many aspects of this service that can indeed come into play. Unfortunatley, right now something like that wouldn't necessarily work for the problem we are facing, that and the big guy upstairs, demands m. overall I just don't want 2 code bases that are exact replicas outside of the controllers themsevles
    – chris
    Oct 26, 2012 at 4:42
  • so basically you want to use the same controller model system but just load different views depending on sub domain? I think the issue with a url rewrite is it wont affect php includes... you could try making a new subdomain vhost entry, using the same site root but just make an alias to point m. to a different view folder. Probably have to rename and also alias the non m. view folder as I don't think an alias can "overwrite" an existing folder
    – WebChemist
    Oct 26, 2012 at 5:03
  • In sorts yes, but all m.example.com traffic I want to use the m.php controller as the controller everything ports through functionality wise. In a sense the closest description would be as if it were the default controller when landing on the site, but acting like index.php does otherwise. If this isn't an option per say, then Ill double up and have 2 code bases I don't care, Im just hoping to find a means of negating that need
    – chris
    Oct 26, 2012 at 5:07
  • wait, hopefully better description.. in a sense, I want everything that goes to m.example.com to route through the m.php controller
    – chris
    Oct 26, 2012 at 5:08
  • I'm not sure why Jon Lim's answer wouldnt work then if you have an m.php controller in the root next to index.php. Sorry I don't have CI setup on a machine I have access to play around with further.
    – WebChemist
    Oct 26, 2012 at 5:18
1

If you already have the m.php controller, you have DNS for m.example.com pointing to the same server, and you have the server setup to host the same document root for both m.example.com and example.com, then this is all you'll need in the htaccess file of the document root:

RewriteEngine On

# this condition ensures the request is for m.example.com
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]

# the usual routing conditions (may need to be changed to suit your specific needs)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# you can alternatively use this
# RewriteCond %{REQUEST_URI} !^/m.php

# route
RewriteRule ^(.*)$ /m.php [L]

# or
# RewriteRule ^(.*)$ /m.php/$1 [L]
# RewriteRule ^(.*)$ /m.php?url=$1 [L,QSA]

This will need to go above any rules that handle routing for your main site.

2
  • Well generally and this is why I ask, the MVC method of accessing the controller itself would be example.com/m where I am trying to basically replace the /m/ part with m. in the beginning of the URL , the actual path to m.php however from root is something like /application/controller/m.php and thats where I am mostly getting confused. Is this concept you provided likely to work in this scenario?
    – chris
    Oct 26, 2012 at 4:37
  • the index.php however resides in the root folder, its all one cluster *** of crazy for me at the moment. Also example.com/application/controller/m.php isn't a means of accessing the controller directly, which furthers the confusion
    – chris
    Oct 26, 2012 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.