Welcome Guest, Not a member yet? Register   Sign In
How to accomplish this > www.mysite.com/username/controller/function/id
#11

[eluser]JoostV[/eluser]
In config/routes.php, make a custom routing for every controller other than user.
Code:
$route['home/:any'] = "home";
$route['comment/:any'] = "comment";

Then, underneath those routing statements, make a route that will send all other uri's to controller users.
Code:
$route[':any'] = "users";

As long as users cannot choose a username like 'comment' or 'home', you'll be fine Smile
#12

[eluser]McNoggin[/eluser]
You could use mod_rewrite to do all the work for you. That way you wouldn't have to change CI at all.
#13

[eluser]codeninja[/eluser]
McNoggin, can you tell how can we use mod_rewrite to accomplish that?

i want to do something specific..

i want to create city specific portal output ..

so write now its www.xyz.com, i want to be able to do city1.xyz.com, city2.xyz.com etc...
#14

[eluser]drewbee[/eluser]
[quote author="McNoggin" date="1226064473"]You could use mod_rewrite to do all the work for you. That way you wouldn't have to change CI at all.[/quote]

Exactly. While my regex skills are a bit one-off, you would do something like the following.

Let's say we have the following:
www.mysite.com/username/controller/function/id

The problem is CI thinks username is the controller when it is not. Heres what I recomend. Identify the fact that it is a user somehow (by a ~ or some other symbol).

So if we wanted to access username, it would be
www.mysite.com/~username

In the regex parse out the ~(username), (controller/function/id)

now, when we are passing these values to the index.php, right now the CI htaccess file passes EVERYTHING. What I would do, is every function will have a second parameter. This is how we will pass the username to it.

Slap the split regex back together: (controller/function/id)/(username)

Now www.mysite.com/~username/controller/function/id is actually passing
www.mysite.com/controller/function/id/username

Then in our controller...

Code:
class Controller extends Controller
{
     function function($id, $username)
     {
         echo $username;    
     }
}

www.mysite.com/~myTestUser/controller/function/id
results in : myTestUser

This symbol being used is near essential. That way, on any 'normal' page that isn't specific to a username, we can still use standard CI functionality.

Current, the following is the part of the htaccess file that does your dirty work.
Code:
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Try this (replace with) ; and yes, their is probobly as issue with the regex even though it is simple.
Code:
RewriteRule ^~(.*)/(.*)$ index.php/$2/$1 [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

I hope this gets you the idea you were looking for. This, IMO, is by far the most elegant solution, and plus, no route/core changes Smile
#15

[eluser]McNoggin[/eluser]
Thanks for filling in the details drewbee. I knew it could be done but I haven't messed with mod_rewrite in so long I was scratching my head trying to remember how to create the rules.
#16

[eluser]jadebot[/eluser]
Hey guys, I am going to hijack my own thread here (kinda). I have been using my hack here to move production along when it finally hit me:

I can use virtual subdomains rather than "mysite.com/user". So ideally it will be user.mysite.com/controller/method. Yes I'm an idiot!

So now after an hour of trying to learn regex I'll appeal to those better than me.
The plan is to use mod rewrite to simply point any subdomain to the main site.
Example:
Code:
user1.mysite.com/blah -> mysite.com/blah
user2.mysite.com/blah -> mysite.com/blah

I can then extract the subdomain from fetching the URL (since the url will still be user1.mysite.com/blah )
and load the user session that way.

If anyone can help me I would greatly appreciate it. I can drop someone $15(us) via paypal or tipjoy as a tip.

So here is a conceptual approach to the rewrite rules:
Code:
IF URL contains subdomain
   remove it, so that it rewrites to mysite.com/....
   also needs to work with the rewrite in CI that removes the index.php
   what I am using is below...
       RewriteCond $1 !^(index\.php|images|robots\.txt|system|js|data)
    RewriteRule ^(.*)$ /index.php/$1 [L]
ELSE
   just rewrite for index.php (I'll load my main site there)

Any help is appreciated. Regex is way over my head!




Theme © iAndrew 2016 - Forum software by © MyBB