Prepending the url path in codeigniter? - php

Generally in CodeIgniter we get this structure of URL :
domain/controller/function
So suppose I went to some user's profile then URL would be :
domain/user/profile/username
How can I change that to
domain/username
OR even more better if
domain/u/username
where the middle part is what i decide.
Any Suggestions?

You can user routes.php for this purpose
Suppose you want a url like
domain/u/username
Then in routes.php you can specify the routing rule like below:
$route['u/(:any)'] = "user/profile/$1";
This specifies that if a url comes like '/u/anyvalue' then execute user/profile/$1
where $1 will be the anyvalue

Related

page before url / change url

I created a login website, and my URL became like this
http://localhost/koperasi/index.php/cukl/index
how to make my url like...
http://localhost/koperasi/index.php/cukl
because I want to make a crud program but I think I have a problem in the url
**
my previous program using http://localhost/lee/index.php/buku so my page can entered CRUD on http://localhost/lee/index.php/buku/ubah
but now my newer program url is http://localhost/koperasi/index.php/cukl/index
how and where to change the url? is in the controller? model? or view?
I think, firstly you have to add an htaccess file which will remove index.php in your URL.
and inside the route.php file, you can define a new URL which you want.
Like this.
$route['cukl'] = 'cukl/index';

seo friendly url for codeigniter

We have a social networking web site for students. Our url looks like this domain.com/student/145236 where 145236 is the roll number. We now have an option of doing something like this domain.com/student/145236/student-name but since google / search engines would prefer domain.com/student-name/student/145236 we would like to do like this, but we are unable to do it, we tried doing this using routes $route['(:any)/student/(:num)'] = 'student/$1'; but it shows 404 page, any help would be grateful :)
This should be work for you:
$route['(:any)/student/(:num)'] = 'student/yourfunction/$1/$2'; 
Here $1 is for ist param (:any) and
$2 is for second param (:num) .
For getting values from the URL inside the student controller you can use CI Segments.

URL rewriting using Codeigniter for specific rule

On my local server, I have setup a website using Codeigniter which opens up fine using below URL
I have a different kind of query regarding rewriting URL
http://localhost/mywebsite
Actually the real URL is below
http://localhost/mywebsite/page/
Which I want it to work as root url as
http://localhost/mywebsite
So I have done it by adding it as default controller in config.php
But now I have another URL as
http://localhost/mywebsite/page/mypagename
Which should open as
http://localhost/mywebsite/mypagename
How can I do it?
You can define a custom route in /system/application/config/routes.php - for example:
$route['abc'] = 'controller_name/abc';
Then, http://mydemosite.com/abc
goes to http://mydemosite.com/controller_name/abc
see https://ellislab.com/codeigniter/user-guide/general/routing.html for more information of URI Routing.
I hope this can be helping you.
I got it solved using routing method as below under config/routes.php
$route['([^/]+)/?'] = 'page/$1';

using different url for a file in codeigniter

Hello I want to change the URL of a page
at the moment it looks like
www.domain.com/privacy
and I want it to look like
www.domain.com/privacy-policy
since it looks much better. Is there a way to achieve this in codeigniter?
CodeIgniter uses the routes defined in config/routes.php to rewrite URLs.
Add a line like this: $routes['privacy-policy'] = 'controller/action';
For full documentation, read the user guide:
http://ellislab.com/codeigniter/user-guide/general/routing.html

Creating short url for multiple controller?

For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.

Categories