I have a url that looks like
mysite.com/index.php/home/user_get
which works fine.
however my client now wishes to have a hashbang in the url
mysite.com/username
Is there anyway to in code igniter allow me to use this type of url whren i logged in?
To get rid of index.php prefix you'll have to enable mod_rewrite in your apache config file and use htaccess to rewrite url.
To enable mod_rewrite, open httpd.conf file and uncomment the following line
LoadModule rewrite_module modules/mod_rewrite.so
To rewrite URLS you have to place .htaccess file (named exactly '.htaccess') into your site root and add the following lines to it:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|scripts|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Third line is important, as it allow static content to be served. You'll have to modify it to match your project structure.
Making user-dependent is impossible, I think. See mod_rewrite documentation for reference
UPDATE: If it is just about usning mysite.com/username
instead of mysite.com/index.php/home/user_get
you could define username
controller, with single method
public function index(){
redirect('/home/user_get', 'refresh');
}
If you going to support several users, e.g. mysite.com/john, mysite.com/dave, that might help
RewriteEngine On
RewriteCond $1 ^(index\.php|images|scripts|css|robots\.txt)
RewriteRule $1 [L]
RewriteCond $1 !^(products|categories)
RewriteRule ^(.*)$ /index.php/users/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
this will route any requests to unknown 'controller' to users controller. You'll have to list all your controllers in (products|categories)
. In the users controller you will have username as parameter, just if your url would be /users/username. Please note that this is not tested.
How to remove the index.php from the URL is well documented elsewhere.
To handle URLs like http//domain.com/myusername, http://domain.com/yourusername, http://domain.com/theirusername
then you can do something like this:
// in config/routes.php
$route['home'] = 'index';
$route['404_override'] = 'user';
Other controllers should continue to work normally, but do a bunch of testing to work that part out.
// in controllers/user.php
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
// detail urls can work as well
// /myusername/posts, /myusername/friends, etc.
$this->$method();
}
else
{
$this->user_home();
}
}
function posts() {}
function friends() {}
function user_home()
{
if($this->uri->segment(2) != FALSE)
{
// this is either a 404, or you have a controller missing
show_404();
} else {
$q = $this->db->get('users');
if($q->num_rows() == 1)
{
// show user info...
} else {
// this is IMPORTANT!!
show_404();
}
}
}
Anyway, that's a starting point for you to work with...(untested code)
I am shocked not one single person has actually responded with a link to the Codeigniter documentation for the Router class. What you want to do is follow the instructions for a custom .htaccess file as supplied by John above.
You only want the following in your .htaccess:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|scripts|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
The Codeigniter URI Routing documentation can be found here. I am presuming by /username you mean you want to be able to redirect different usernames to different pages, to a profile page or something.
In your application/config/routes.php
file add the following lines:
// Insert any other routes you don't want hijacked here
// Example - a contact page.
$route['contact'] = "pages/fetch/contact";
$route['(:any)'] = "controller/method/$1";
The $1 means capture the value from (:any) and supply it as an argument to your controller function. If you want to be able to link to other sections of the site like a contact page and it just so happens to be located at: /contact - then you'll have conflict issues with the URL location being hijacked.
No need to do anything else, or add anything to any controllers.