PHP URL Routing; Variables to 'directory' - php

Is it possible to change
host/index.php?variable=3
to
host/index.php/3
In my instance I'm using
host/bot?id=3
But I'd like it to be
host/bot/3 <-- This will be the only variable EVER used
How can I accomplish this, any links or code you can provide?
Thanks in advance!

GET /hotels/:id Hotels.Show
Segments of the path may be matched and extracted with a : prefix.
The :id variable above will match anything except a slash. For example, /hotels/123 and /hotels/abc would both be matched by the route above.
Extracted parameters are available in both the
Controller.Params map
and via Action method parameters.
https://revel.github.io/manual/routing.html

Related

I want to remove controller as well as function name from url

I need help to rewrite url and remove controller as well as function name from url given url is my current uel.
URL :http://localhost/example/Loadercontrol/detail/200
I want this type of url
URL : http://localhost/example/name (replace id with name).
I already try rewrite url as well as
$route['example'] = 'localhost/example/Loadercontrol/detail';
but it's not working. I've really tried to fix the issue. Please help me.
Try like this..
$route['example/(:num)'] = 'example/Loadercontrol/detail/$1';
For details see more here...https://www.codeigniter.com/userguide3/general/routing.html#wildcards
Wildcards are actually aliases for regular expressions, with :any being translated to [^/]+ and :num to [0-9]+, respectively.

Codeigniter dynamic variable routing

I am looking to dynamically re-route urls based on a parameter within the uri segment. For example if I have the following url:
domain.com/account/message/action/view
How could I go about creating a route that checked to see if the uri contained "action" and if so re-route to the value of the function + '_' + value of action?
domain.com/account/message_view
I've been reading through the routes documentation provided by ellislab and am still not quite sure how to go about doing something like this or if it would be better to use server rewrite rule? Any information greatly appreciated!
You would likely want to use the regular expressions availability in routes. Something along the lines of:
$route['([a-z]+)/action/([a-z]+)'] = "$1_$2";
Of course, that may not be exactly what you are looking for, but it should get you started.

Parenthesis issue in codeigniter url

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!

Creating SEO canonical paths (none absolute)

I am using my code like this to generate a canonical link for SEO purposes
<link rel="http://www.mydomain.com$_SERVER[SCRIPT_NAME]">
So lets say the file I go to is http://www.mydomain.com/thisfolder/?this=that&yes=no
The canonical link will display as
http://www.mydomain.com/thisfolder/?this=that&yes=no
What I want is no matter what the extra variables being passed in the URL are that it will display the canonical as
http://www.mydomain.com/thisfolder/
I have tried both REQUEST_URI and SCRIPT_NAME in my $_SERVER[]; but both do the same thing. Is there a way I can achieve this whether I am just not using the correct name to $_SERVER[]; or is there anyway to do this?
Try $_SERVER['PATH_INFO']
Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
http://php.net/manual/en/reserved.variables.server.php

How to make parameter name in URL default and hidden in Kohana?

I'm trying to make this work. I want my routing to behave like this:
when I type URL ie. example.com/api/getpage/http://smth.com I want to retrieve in my action id parameter with http://smth.com value.
My routing for this case looks like this right now:
Route::set('api', 'api/<action>/<id>')
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
And what's in action:
public function action_getpage()
{
$obj = $this->scrape($this->request->query('id'));
$this->response->body(json_encode($obj));
}
Now URL like this example.com/api/getpage?id=http://smth.com works like a charm, but I don't want it that way. Is there any way to achieve that goal? Thanks in advance for all suggestions.
The problem is you can't use slashes within a url segment (except in get parameters). This has nothing to do with kohana routing but how your web server handles it. The only way around it would be to replace he slashes with something else. You'd also encounter issues if the url you pass as the final segment had get parameters on the end of it, the question mark would be cause it to be treated as get parameters of your main url, not the final segments.
Another option would be to base64 encode the final url segment then decode it inside your controller. This would get rid of any slashes and question marks. Php has simple base64_encode and base64_decode functions. The only downside is tha portion of the url will look like an arbitrary garbled string. http://php.net/manual/en/function.base64-encode.php

Categories