Redirection rules in codeigniter - codeigniter

I want to create location pages in my codeigniter site. So I have one contrller named locations and index method. So all the requests http://mysite.com/location_name should be landed to http://mysite.com/index.php/locations/index. And all other should work as it is like http://mysite.com/login should be landed to http://mysite.com/index.php/home/login. Contact us http://mysite.com/contact-us should be landed to http://mysite.com/index.php/home/contact.
I tried to achieve this by writing following line route rule (route.php):
$route['(:any)'] = 'locations'; //location name can be anything around the world
So locations are working fine, but http://mysite.com/login and http://mysite.com/contact-us are not working, they redirecting continuously in infinite loop.
Please suggest the solution. Thank.

The routes are applied from top to bottom, so you need to have your more specific rules listed first, then your more generic rules last:
$route['login'] = 'home/login';
$route['contact-us'] = 'home/contact';
$route['(:any)'] = 'location/index';
I see that you mentioned you've tried changing the order of rules in your route file. So If you have done this and it's not working - you have something else going on.
I would check these things:
.htaccess file (if you're using it)
controllers that are causing the infinite loop (any redirections in there, _remap method, etc. )

Related

codeigniter routing issue

I had my users profile at
www.domain.com/user/username
and moved it at
www.domain.com/username
but this required to add most classes functions into the routes.php file in the config and if i want to add new features to my app i will need to add all the functions into the routes.php file which doesnt sound good practice...
What is the best way that other deal with it on CodeIgniter ?
Perhaps, you can do it the other way round - make a whitelist of usernames that can't be taken (those would be names of your controllers, like admin, contact, etc...) and route anything except the whitelist items.
seems i got the answer
what i did is add the below code for every controller i have
$route['controller'] = "controller";
$route['controller/(:any)'] = "controller/$1";
and this code at the bottom
$route['(:any)'] = "user/$1";

Additional URI segment before Codeigniter controller

I'm making this little project in Codeigniter and I need to have one additional segment before controller.
The default Codeigniter URL is:
base_url/controller/function/parameter1/../parametern
What I need is:
base_url/something/controller/function/parameter1/../parametern
Where "something" is a name that user creates so I cant create a subfolder in controllers folder as suggested in other topics because its dynamic and each user chooses what he likes.
Basically what I need is that it like ignores that segment (I would catch that segment in a hook, extend CI_controller or something like that and validate it), for example if I write base_url/stackoverflow-rocks/home/function, base_url/asd-whatever/home/function Codeigniter would look at it like base_url/home/function
I have also looked at Passing variables before controller URI segment in CodeIgniter question, its almost the same as mine but the suggested answer didn't work.
I used $route['(:any)/(:any)'] = '$2'; which works if the url is base_url/whatever/home, but if the url is base_url/whatever/home/function or base_url/whatever/home/function/param1/../paramn it doesn't, the workaround for this is to write:
$route['(:any)/(:any)'] = '$2';
$route['(:any)/(:any)/(:any)'] = '$2/$3';
...
$route['(:any)/../(:any)'] = '$2/../$n'
which is easy, but seems a bit lame, and it will only work if I have from 2 to n segments in url. I also tried to mix it with regular expressions, like:
$route['(:any)/(.*)'] = '$2'
But it works just like the previous one, (.*) is just one segment but not the whole url...
Is there some way to just write
$route['(:any)/rest of the url'] = 'rest of the url';?
I also tried using .htaccess:
RewriteRule ([^/]*)/(.*) inedx.php?/$2
or
RewriteRule (.*)/(.*) index.php?/$2
But it didn't work because $2 referenced to the first part, for example if I write:
base_url/one/two it rewrites it to base_url/index.php?/one.
But if I use:
RewriteRule (.*)/(.*) index.php?/$1/test/$2
it references as it should, for example base_url/one/two => base_url/index.php?/one/test/two, so $1 = one and $2 = two, as it should be.
Quite a long question but I hope someone can help, thanks!
see if this works:
$route['base_url/controller/(:any)'] = 'base_url/$1'

CI: Controllers in subfolder - removing subfolder from url

I want to group my controllers, views and models into public/ and members/ subfolders.
But for the public stuff, I don't want /public/ to show in the URL, so:
http://mysite.com/ & http://mysite.com/section/
should point to: /public/home & public/section/
How should I change routes.php to accommodate this?
(I'm fine with members/ stuff having members/ in the url)
To make http://mysite.com/ point to /public/home you will need to mark public/home as your default controller. You will also need to add a specific route to make http://mysite.com/section point to /public/section. Try this (the regex is a little dubious in my opinion, but may do what you want):
$route['default_controller'] = "public/home";
$route['section/(.*?)'] = "public/section/$1";
Kindly see if this works for you:
$route['section'] = "public/section";
Your index page would remain as "public/index.php"
You don't need to add routes for every controller.
This works for me:
$route['members'] = 'members'; // route members to members
$route['members/(.*?)'] = 'members/$1'; // route members/... to members/...
$route['(.*?)'] = 'public/$1'; // route anything but above lines to public/...
The first two lines are intentionally redundant to protect 'members' segment from being routed to 'public'. And the third line does the magic.

code igniter routing

I am trying to work out the code igniter routing
I have a url that will look like
http://example.com/register
I want that to hit
http://example.com/account/register/normal
I also have
http://example.com/invite/something_random_here
which I want to hit
http://example.com/account/register/invite/something_random_here
I also have
http://example.com/register/something_random_here
which I want to hit
http://example.com/account/register/subscribed/something_random_here
How do I setup these routes?
Pretty much Straight out of the User Guide
$route['register'] = "account/register/normal";
$route['invite/(:any)'] = "account/register/invite/$1";
Basically anything after invite/ will get tacked onto the end of account/register/invite. I believe it only works for one segment, if you want multiple segment support you'll have to use a regular expression:
$route['invite/(.+)'] = "account/register/invite/$1";
Another usefull one (because I believe (:any) only works for characters) would be:
$route['invite/([a-zA-Z0-9_-]+)'] = "account/register/invite/$1";
This will let all alpha-numeric values (single segment) with _ or - go through, great for GUIDs :)
I believe for your first URL, you would use the following route:
$route['register'] = "account/register/normal";
Your second URL, would make use of the following route:
$route['invite/(:any)'] = "account/register/invite/$1";
Both of these routes would need to be placed in your "config/routes.php" file.
Hope this helps?

SEO-friendly URLs in CodeIgniter without the use of slugs?

Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Access from the SEO-friendly URL should be allowed, but access via the integer should cause a 404 error.
This should also work dynamically, where any integer is automatically converted to a URL-safe version of its corresponding category name.
I've seen a few solutions that use 'slugs'... is there a decent alternative?
Thanks.
I've only been working with CodeIgniter for the past couple of months in my spare time, so I'm still learning, but I'll take a shot at this (be gentle):
There is a url_title() function in the URL Helper (which will need loaded, of course) that will change Foo Bar to foo-bar.
$name = 'Foo Bar';
$seo_name = url_title($name, TRUE);
// Produces: foo-bar
http://codeigniter.com/user_guide/helpers/url_helper.html
The URL helper strips illegal characters and throws in the hyphens by default (underscores, by parameter) and the TRUE parameter will lowercase everything.
To solve your problem, I suppose you could either do a foreach statement in your routes.php file or pass the url_title() value to the URL, rather than the ID, and modify your code to match the url_title() value with its category name in the DB.
Afaik the link between 4 and "foo-bar" has to be stored in the DB, so you'll have to run some queries. This is usually not done via routing in CI. Also routing just points a URL to a controller and function and has little to do with url rewriting.
Why don't you want to use slugs?
You could try storing the search engine friendly route in the database using this method or this one.
I wouldn't recommend throwing a 404. Use the canonical link tag in the instead if your worried about Google indexing both http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html.
But if you really want to I guess you could write a function that is called during the pre_controller hook http://codeigniter.com/user_guide/general/hooks.html that checks to see if the URL has an integer as the second segment then call the show_404() method. Perhaps a better solution when writing this function would be to redirect to the SEO friendly version.
Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Yes.
Using CI 3,
http://www.codeigniter.com/user_guide/general/routing.html
Use Callbacks, PHP >= 5.3
$route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
{
return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
You can route to call a function to extract the name of the category.
Hope I answered your question and can help more people to like codeigniter as I believe it's speedy and light.
Slugs usage is important to make web application more secure which i think is important.
A better recommendation will be to use route to give you a better solution.
$route['(:any)/method/(:num)'] = 'Class/method';
or
$route['(:any)/method/(:num)'] = 'Class/method/$1';
$route['(:any)/gallery/(:num)'] = 'Class/gallery/$1';
base_url()/business-services/gallery/6
base_url()/travel/gallery/12
how to modify routes in codeigniter
Have fun :)

Resources