Parenthesis issue in codeigniter url - php

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!

Related

Redirect & Route wildcard URL - with underscores to hyphens

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...

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.

codeigniter route problem

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

using seo user friendly in php

this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).

htaccess mod_rewrite

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.

Categories