I am trying to set a custom route for friendly url when loading a tag, by that I mean that I want to convert this: http://test.com/en/results?search_query=data&search=tag to this http://test.com/en/tag/data
Tried to do so by setting this in my routes.php:
$route['^(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$1&search=tag";
But no success, am I doing it wrong?
My routes.php:
$route['default_controller'] = "home";
// URI like '/en/about' -> use controller 'about'
$route['^(en|es|ro)/video/(.+)$'] = "video/id/$2";
$route['^(en|es|ro)/results$'] = "fetch/results$2";
$route['(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$2&search=tag";
$route['^(en|es|ro)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|es|ro)$'] = $route['default_controller'];
$route['404_override'] = '';
I’m fairly sure the
^is not necessary. Try without it:In that context,
^will be treated as a literal string. If you wanted top use a regex, I believe you’d have to wrap the regex itself in parentheses, like so:I’m not 100% certain on that, but if I’m correct both should work. However, routes always assume the first part of the pattern is what the string should start with, so the
^shouldn’t be necessary anyways.One more thing: You aren’t using the second match, but the first. I believe you want to replace
$1with$2in your route.EDIT: Upon further investigation and testing, it seems that Codeigniter cannot properly handle routes that use a query string like
?key=value. If you use a slash before the?question mark, you can at least get it to route to the right controller and method:A url like this:
…will be routed to the
resultsmethod with this string as an argument, but$_GETwill be empty:So, you could pick apart the argument with
parse_str():You could always use
.htaccessof course, but I understand the desire to keep all the routing logic in one place. Sorry if this isn’t the answer you hoped for, but I’m afraid that without writing a custom router it’s not possible to do this the obvious way in Codeigniter. My suggestion would probably be to find another way to handle your routing and the way you’re accessing data from the URL.