i have following route in route.php in codeigniter
$route['^(?!login|signup|autos|jobs|jobwanted|admin).*'] = "pages/bpages";
it is redirecting everthing to pages/bpages except (login|signup|autos|jobs|jobwanted|admin )
eg www.mysite.com/sohailanwarpk
as sohailanwapk is not in (autos|jobs|jobwanted|admin) it will redirected to pages/bpages
the problem i am facing is in that condition if my url is like
eg www.mysite.com/autosss
eg www.mysite.com/jobs123
it should direct autosss or jobs123 to "pages/bpages"; but its not,so how can i exaclty match (autos|jobs|jobwanted|admin) so that everything else will be redirected.
Split it into 2 routes:
$route['^(?!(login|signup|autos|jobs|jobwanted|admin)).*'] = "pages/bpages";
$route['^(login|signup|autos|jobs|jobwanted|admin)[^\/].*'] = "pages/bpages";
Just write 2 rules:
$route['^(login|signup|autos|jobs|jobwanted|admin)$'] = '$1';
$route['(:any)'] = 'pages/bpages';
Related
I've made a page /module/hello/?id_category=123, and I need to make the id_category rewritten to a name such as 123 would = abc.
Is there a way to achieve this without making 1000 manual redirects in the htaccess?
I know this can be done with a RewriteRule [L], but as far as I know you can't ask the database to convert the id to a name and then tell htaccess to rewrite the URL.
Thanks,
Luke
Use the below function and add id_category as string, stringToNumURL function returns you it's numeric mapping.
Then just include your URL content like this.
<?php
//http://mypage.com/module/hello/?id_category=abc
$str_arr = stringToNumURL($_GET['id_category']); //return 123
include_once("http://mypage.com/module/hello/".$str_arr);
function stringToNumURL($url){
$arr = array('a','b','c');
$arr_values = array_flip($arr);
$url = str_split($url);
$url_to_call="";
foreach ($url as $value) {
$url_to_call.=$arr_values[$value]+1;
}
return $url_to_call;
}
I want to remove some url in CI
if any echo like:
www.blabla.co/content/5DRwA/6Yt54/bla-bla
so, replace to:
www.blabla.co/content/bla-bla
nb: 6Yt54 and 5DRwA is a random value
How I remove URI Segment 2 from behind like that?
How can I solve it?
Try the uri class. First retrive uri with:
$array = $this->uri->segment_array();
Then remove second segment:
unset($array[2]);
Unfortunately your url doesnt seem to follow CI convention (you could use then $this->uri->assoc_to_uri($array)). So iterate over array to create uri.
$my_uri = ''; //better name it $my_uri not to conflict with $this->uri
foreach ($array as $value) {
$my_uri.= '/'.$value;
}
And then you can use your new uri f.e. to redirect:
redirect($uri);
My current URL is like:
www.hostname.com/get_city/
(this url is already shorten by using routes)
Here get_city is my method name and now what i exactly want is...
1)remove controller name from url
2)pass selected city value from dropdown and set in URL as a parameter
So,Required url is: www.hostname.com/california OR www.hostname.com/newjersey
NOTE:I know how to use routes but in such case how to make dynamic URL?!
AND please don't give me direct reference of ellislab docs because i have already tried those things
For dynamic route in codeigniter:
try like this:
in your routes.php file copy and paste these codes:
require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db -> get('news');
$result = $query -> result(); //result return all records
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subject)));
$route[$string] = "controller/news_details/$row->id";
}
so you can , change $string with any string that you want.
then try type new url and see Routes will work fine.
NOTE:.htaccess file must be removing index.php in url
hope this help.
You can try with the method "_remap".
Check the official documentation for more info about the behavior of the function:
http://www.codeigniter.com/user_guide/general/controllers.html
Regards.
You need to config routes in application/config/routes.php
After existing routes add
$route['([a-z]+)'] = 'controller_name/method_name/$1';
But this overwrite all routes and before this route you need to declare all routes for you controllers
$route['product/:any'] = 'product/$1';
$route['catalog/:any'] = 'catalog/$1';
and after
// this route be used when previous routes is not suitables
$route['([a-z]+)'] = 'controller_name/method_name/$1';
DOCS:
http://code-igniter.ru/user_guide/general/routing.html
CI2: URI Routing
CI3: URI Routing
This is my code in routes.php
$route['default_controller'] = "admin";
$route['(:any)'] = $route['default_controller']."/index/";
This is my url:
http://myserver.net/visio/jklmn
But i cant to get the value in index() in admin controller.
I want to get the value jklmn in admin controller.If there is any mistake in my routing code.
This is my index() code;
function index($key = ""){
if(isset($key)){
$newkey = $key;
$data['key'] = $key;
$this->load->view('index',$data);
}else{
redirect('admin/index_login');
}
}
When i taking above link in browser i get the error message below:
Not Found
The requested URL /visio/jklmn was not found on this server.
Use this routing rule:
$route['(:any)/(:any)'] = $route['default_controller']."/index/$2";
which will match a URL with 2 segments (each containing any character) and pass the second match as $2.
You can also pass the first match, just use $1.
Is it possible to create a wildcard match using the Zend Framework Zend_Controller_Router_Route_Hostname for the actual domain? I tried the simple example below but the system would not recognize the route. When I hardwired the route (login.domain.com), it would work properly.
resources.router.routes.login.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.login.route = "login.*"
resources.router.routes.login.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.login.chains.index.route = ":action/*"
resources.router.routes.login.chains.index.defaults.controller = "login"
resources.router.routes.login.chains.index.defaults.action = "index"
$route = new Zend_Controller_Router_Route_Hostname ('login.:domain.:net');
$_SERVER ['HTTP_HOST'] = 'login.example.com';
$request = new Zend_Controller_Request_Http ();
$match = $route->match ($request);
var_dump($match);
Is this possible without the :domain.:net both being explicit, i.e. without stipulating just one period?
i.e. I currently have
new Zend_Controller_Router_Route_Hostname('sub.example.com',array('controller' => 'x'));
..but what I'd really like to do is:
new Zend_Controller_Router_Route_Hostname('sub.:remainder',array('controller' => 'x'));
..whereby this route will match any hostname that begins with 'sub.' including sub.example.com, sub.another.example.com, sub.somethingelse.com, sub.com etc.
Doesn't seem to work though!
Anyone got this working?