result I want :
greeting/102/steve => greeting/index/102/steve
in greeting.php :
function index($order,$name)
{
echo "order: $order , name : $name ! ";
}
in route.php :
$route['greeting/(:num)/(:any)'] = "greeting/index/$1/$2";
result I get :
order : , name : steve !
Actually, it's right to use double quotes. It's even indicated like this in the manual (beside having done it a hundred times), so I don't see the problem #cwallenpool is pointing out.Your routing looks fine, be sure it is called after the reserved routes
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['greeting/(:num)/(:any)'] = "greeting/index/$1/$2";
.
I suggest you to try using $this->uri->rsegment(n) (info on user guide here) to catch the rerouted uri segment that's causing you trouble. (similar to $this->uri->segment(n) but designed specifically for rerouted URIs)
You can also try changing the $config['uri_protocol'] from AUTO to PATH_INFO (or one of the other alternatives) and see if the problem doesn't sit there. Remember also to delete the 'index.php' part in $config['index_page'] if you're using htaccess to delete the index.php from you URL.
I have recently written a library that can provide ease in such cases. I pass the values to the required variables via name rather numbering. Also the names are included in routes to easy reference them.
May be you can have a look at it, could be useful in your case.
https://github.com/aajiwani/LaravelRoutingForCodeIgniter
Related
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 2 years ago.
I have a Home.php controller and terms method in it. I have a url like this: https://www.example.com/terms and it works fine.
My routes.php:
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['terms'] = 'home/terms';
Now, I need to change the url to be like this: https://www.example.com/#/terms Notice the "#" in-between.
I tried adding this to routes but it didn't work:
$route['#/terms'] = 'home/terms';
The above change show home page when I open https://www.example.com/#/terms
However, if I put any other permitted character $config['permitted_uri_chars'] mentioned in conifg.php in place of # then it works fine:
$route['a/terms'] = 'home/terms'; // this works: https://www.example.com/a/terms
$route['2/terms'] = 'home/terms'; //this works: https://www.example.com/2/terms
I also added # in $config['permitted_uri_chars'] but that too didn't work. Keeps showing home page only.
Also, can anyone explain me why # is not treated the same way as other characters in permitted_uri_chars?
Controller is Home.php and method is terms(). Using CodeIgniter version 3.1.11
Thank You!
EDIT:
I need to know if I can achieve it using Codeigniter's routing? Or maybe some rewrite rule using .htaccess?
This is caused by the web browser, not CodeIgniter. Web browsers treat # and anything after it in a URL to be a reference to an element on the web page pointed by the URL before the # part.
So for a URL https://example.com/#page1, the web browser will make a request to https://example.com/ and then look for an element with ID page1 on the web page.
If you want different pages to load by using a '#' in your URLs, it's done using front-end frameworks like Angular. Check Angular's routing features to understand how it makes the browser load a different view for different URI paths after #.
Few of my project urls contains the parenthesis and it is a codeigniter project. Unfortunately I cant remove them.
So I am writing following rules in my route.php of config folder:
$route['abc-(def)'] = 'locations/index/12492';
$route['abc-%28def%29'] = 'locations/index/12492';
$route['404_override'] = 'home/pagenotfoundhandler';
So when I am trying to access URL http://mydomain.com/abc-(def) or http://mydomain.com/abc-%28def%29 it takes me to home/pagenotfoundhandler, while it should take me to location controller and index method. Please suggest how to deal with parenthesis in codeigniter.
The parentheses are a part of the regex pattern, so try escaping them if you want to use them as ascii characters like this:
$route['abc-\(def\)'] = 'locations/index/12492';
And one more place to look:
There should be a allowed uri characters setting in config.php for your codeigniter system. check there if parentheses are allowed.
Alternatively, you can disable $config["csrf_protection"] to skip url xss cleaning.
Well I guess that is because the page http://mydomain.com/abc-(def) which is supposed to redirect to locations/index/12492 can not be found! so a 404 - Page not Found error takes place, which of course as you have defined takes you to home/pagenotfoundhandler! make sure the path exists or even why do not you give the complete path to see if it works!
i tried to find solution, but i think i am quite doing something wrong here,
i hope any one with good knowledge of codeigniter routing can help me in this regard.
what i want is that,
i have default controller named main
$route['default_controller'] = "main";
$route['404_override'] = '';
my site urls are like
mydomain.com/main/#home
mydomain.com/main/#search
mydomain.com/main/#login
what i want is to remove/hide the main from center and links can directly work.
like this
mydomain.com/#home
however i did succeeded a little using codeigniter documentation.
This is what i did to achieve it.
in route file i added this
$route['(:any)'] = "main/$1";
it worked but it messed up with other links.
First now domain.com/main/#home stopped working.
second, now i cant have other controller name here??
i mean if i try this domain.com/virtualvault/#search, it wont work..
What i am trying to have that,
When i go to domain.com/main/#home it should go to home page
and also when i try domain.com/#home it should also go to same home page
not to forget that i want other controllers to be working fully.
i mean, domain.com/othercontrollername/#function should work.
what and how to achieve it, .htaccess or route php file..
i even tried this below route code.
$route['#+(:any)'] = "main/#+$1";
but i am no good in this routing or .htaccess files.
any ideas how to achieve it??
// The below line is for the old controller to work as before
$route['main/(:any)'] = "main/$1";
//This line is for making other controllers work as before, you have to put them all
$route['virtualvault/(:any)'] = "virtualvault/$1";
//This is the last line; the order is important
$route['(:any)'] = "main/$1";
If you need only those three URLs (home, search and login) to work without the controller name, just put the three of them in routes.php ($route['login'] = "main/login";, etc.) and use no wildcards in order to prevent other URLs from getting rerouted.
So, all of them should be:
$route['home'] = "main/home";
$route['search'] = "main/search";
$route['login'] = "main/login";
I'm using CodeIgniter and I needed to redirect several urls with underscores to their equivalent with hyphens.
/some-controller --> /some_controller
I've partially solved this issue by tweaking the config/routes.php file.
The thing is :
How would I expand that to a Controller's function WITH a parameter.
Let's say I've got a controller some_controller (some-controller redirects to some_controller) and a function func in it (optionally taking a param).
$route['some-controller'] = 'some_controller';
$route['some-controller/func'] = 'some_controller/func';
This works. But, what if I have some-controller/func/someparam. (and someparam can be anything).
How could this redirect be implemented?
Basically what I need is a redirection from :
some-controller/func/*
to
some_controller/func/*
Any ideas?
Hint :
I don't need anything complicated like this one (
How to replace underscores in codeigniter url with dashes? ).
Hadn't searched enough - I admit it (it's one of those issues that always seem more complicated than they should be) :
http://codeigniter.com/user_guide/general/routing.html
$route['some-controller/func/(:any)'] = "some_controller/func/$1";
Or you could use (:num), if what you're expecting is a number...
I'm trying to put something with this, whenever I go to a page like:
http://www.example.com/character.php?id=3
I want the mod rewrite to change it to:
http://www.example.com/character/Jim_Carrey
Which of course, the ID is the row of the character name...
For that kind of example... I've tried to work with it, but don't seem to get most of the production of htaccess because I haven't worked with .htaccess a lot really.
It's actually the other way around:
On your website, you write all your links the way you want them to look. For instance http://www.site.com/character/Jim_Carrey
In your database, you add a field,
commonly called "slug" or
"post_slug" which refers to the
Jim_Carrey" part of the url. IT MUST
BE A UNIQUE ELEMENT, much in the way
of a primary key. So make sure you
have a function that does take care
of creating the slug based on a
given string (the post title for
example) and making sure there is no
duplicate.
Then in your .htaccess (on the root folder) you do
something like
RewriteEngine On
RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]
Finally, in your character.php
script, you do a database query not
against the ID, but against the
post_slug field.
If you're only using the character's name, then something like the following would do
RewriteRule ^character/(.*)$ /character.php?slug=$1
with a URL of eg http://www.example.com/character/Jim_Carrey. You'll then need to look up the character's name in the database using the slug passed in, as you won't have the ID to look it up with.
Alternatively you could include the ID in the URL if you need it, vis:
RewriteRule ^character/([0-9]+)/.*$ /character.php?id=$1
This way you could have a URL like http://www.example.com/character/3/Jim_Carrey which would include the character name (for SEO reasons etc etc), but also the ID which you could then look up directly in your database.
Edit a small PHP example for you re the first one:
<?php
// ... database connection stuff here etc
$slug = $_GET["slug"];
// IMPORTANT: perform some data sanitization here
// I'm just going to make sure it's only letters, numbers and
// hyphens/underscores as an example.
$slug = preg_replace("/[^a-zA-Z0-9\-_]+/", "", $slug);
// Now look up in your database
// Ideally you'd have a slug column to compare this with, that you can fill
// when your record is created/updated
// You'd also be best off using bound parameters here, rather than directly
// adding the data into the query string. I personally use the MDB2 PEAR
// module but feel free to use whatever you normally use.
$qry = "SELECT * FROM characters WHERE slug='" . $slug . "'";
$result = mysql_query($qry, $db);
// do something based on this result, fail if none found
// or show details if OK etc
// ...
?>
Hope this helps! As always, use bound parameters where possible for your queries, and perform sanitization of your user data well. The PEAR MDB2 module has a nice page on how to do this here.
Edit 2 a quick and dirty setup :-)
.htaccess file as follows:
RewriteEngine On
RewriteRule ^character/(.*)$ /character.php?slug=$1
Your .htaccess file would ideally be in the root of your site. Eg /home/wayne/public_html/ or wherever your index file is served from
A URL to match that would be http://www.example.com/character/Jim_Carrey - with the phrase "Jim_Carrey" appearing in your $_GET array as $_GET["slug"]. NB apologies, wrote that PHP sleepy last night above so no wonder $_POST wouldn't work as its a GET request :-) I've updated it now!
Finally you need to make sure that your host supports the use of .htaccess files. The setup of this is out of the scope of SO so any Apache configuration questions you'd be best asking over at https://serverfault.com/
I'm fairly certain you can't do this through htaccess. You'll need to do it in PHP, querying the database using the information from the url (?id=3) and then calling the Header Function using what you've learned from the database.
Sounds like you might be able to use mod_rewrite rewritemap.