![]() |
Lovely User URLS (http://domain.net/~Username) - 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: Lovely User URLS (http://domain.net/~Username) (/showthread.php?tid=14877) |
Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]IamPrototype[/eluser] How can I make URLS like http://domain.net/~Username, http://domain.net/username etc., and make it redirect to a users profile. I know CI is build with routes, but do I have to do some reg. exp or? I hope somebody will help me (it's for my community). ![]() Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]freshface[/eluser] Or use remap in your controller function _remap() { $usr = $this->uri->segement(1); if($usr) { redirect('…'); } else { echo 'no username passed'; } } Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]Phil Sturgeon[/eluser] It wouldnt get to the controller, as the username would be used as the controller name and a 404 would be returned. You could use routes, but then EVERY 1 uri segment will be checked as a username. The way I like to do this is to add a URL suffix in the config file (meaning add .html or similar to all your pages) then use the following mod)_rewrite rules: Code: <IfModule mod_rewrite.c> That is basically a standard CI .htaccess with an extra rule in it. Any URI segment 1 with a-z, 0-9, - or _ in it which does NOT have a URL suffix will go to a controller which will take the username and show the correct profile. If you have mod_proxy enabled you could even swap the flag [L] with [L,P] to make the URL stay as example.com/username. Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]IamPrototype[/eluser] Where do I place it? So it has to be like: Code: function _remap() { I don't need an else-statement on my if(), because I only want the user to redirect IF the URL is something like http://domain.net/Username Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]IamPrototype[/eluser] [quote author="pyromaniac" date="1232390860"]It wouldnt get to the controller, as the username would be used as the controller name and a 404 would be returned. You could use routes, but then EVERY 1 uri segment will be checked as a username. The way I like to do this is to add a URL suffix in the config file (meaning add .html or similar to all your pages) then use the following mod)_rewrite rules: Code: <IfModule mod_rewrite.c> That is basically a standard CI .htaccess with an extra rule in it. Any URI segment 1 with a-z, 0-9, - or _ in it which does NOT have a URL suffix will go to a controller which will take the username and show the correct profile. If you have mod_proxy enabled you could even swap the flag [L] with [L,P] to make the URL stay as example.com/username.[/quote] Thanks, I guess this example is better for use. So everything I have to do is write that into my .htaccess and then it should work by itself? ![]() Etc http://domain.net/IamPrototype turns into http://domain.net/profiles/view/IamPrototype Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]freshface[/eluser] Inside your controller. Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]freshface[/eluser] What about routes? $route['^(.*)$'] = "user/view/$1"; Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]IamPrototype[/eluser] That route would do it like this: http://domain.net/IamPrototype -> user/view/IamPrototype Without any more code? Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]Phil Sturgeon[/eluser] Yes IamProtoype, all you need is to set yourself a url suffix and add that .htaccess. Nothing else required. Blogged ![]() Lovely User URLS (http://domain.net/~Username) - El Forum - 01-19-2009 [eluser]IamPrototype[/eluser] Thanks a lot ![]() |