CodeIgniter URI routing with "#" as path parameter [duplicate] - php

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

Related

how to get the actual url in php while all pages are routing through index.php in MVC frameworks

When routing, all the php scripts request_uri showing only index.php.
I want to find the actual uri instead of index.php.
So,is there any php functions or variables that helps in getting the actual url.
I know they are different ways of getting the url for different frameworks but i want to know solution that works for every framework
Since you stated that you are using laravel, the solution below only works for this particular framework.
$current = url()->current(); // Returns the current url
If you need to pass parameters, getting the name of the route and building the url based on it works fine:
$current = URL::action(Route::currentRouteName(), ['arg' => 'value']); // Returns the current url and assign arguments to it

how to remove the # in my url [duplicate]

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 6 years ago.
https://progressive.e-inkasso.dk/login#application/views/ajax/dashboard.php
when my webpage loads there is this # and not a / i tried to route it to be only dashboard or login in the route.php file but there was no way so far that it worked.
$route['default_controller'] = "Register";
$route['404_override'] = '';
$route['login/(:any)/(:any)/(:any)/(:any)/(:any)'] = "Register/login";
$route['(:any)/dashboard'] = "Register/index/$1";
//$route['(.*)/login'] = "Register/login/$1";
$route['(.*)/register'] = "Register/register/$1";
and i have no clue how to fix it
I think it has something to do with AngularJS and its routing integrated in your website. This is not default behaviour of CodeIgniter. I inspected your page's source code and it seems to load some Angular Javascript things.
Didnt bother to munch to find what and where exactly. But my best guess is that AngularJS is doing this. You should probably redefine your question but i doubt it is possible to remove the trailing #. Good luck

URL rewriting using Codeigniter for specific rule

On my local server, I have setup a website using Codeigniter which opens up fine using below URL
I have a different kind of query regarding rewriting URL
http://localhost/mywebsite
Actually the real URL is below
http://localhost/mywebsite/page/
Which I want it to work as root url as
http://localhost/mywebsite
So I have done it by adding it as default controller in config.php
But now I have another URL as
http://localhost/mywebsite/page/mypagename
Which should open as
http://localhost/mywebsite/mypagename
How can I do it?
You can define a custom route in /system/application/config/routes.php - for example:
$route['abc'] = 'controller_name/abc';
Then, http://mydemosite.com/abc
goes to http://mydemosite.com/controller_name/abc
see https://ellislab.com/codeigniter/user-guide/general/routing.html for more information of URI Routing.
I hope this can be helping you.
I got it solved using routing method as below under config/routes.php
$route['([^/]+)/?'] = 'page/$1';

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.

How can I create a URL rewrite similar to Wordpress in my own PHP app

I am working with PHP5.3.6 on Windows 2008R2.
In Wordpress, I can set all kinds of friendly URLs which ultimately route to one PHP page. From the outside, it looks like many pages, but is only one.
In the web.config file (or .htaccess) file, there is only one instruction as opposed to having one entry per page/article.
This means that somewhere PHP is looking at the URL, comparing it to the database (where the article titles exist) and then routing transparently to the page.
All of this is transparent to the end user.
How is this accomplished in a non wordpress PHP site?
Here's a related answer that touches on how to do that. In brief, you'll want to check the $_SERVER['REQUEST_URI'] and just parse that.
Here's a simple example of parsing the request (MVC routers are usually configurable, and can route and match for many different URI structures):
If your format is something like news/article-slig you can do this (example code, there are less rigid ways to do this):
list($section, $slug) = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
At this point your PHP script knows how to interpret the request. If this were a full blown MVC application, the router would load the matching controller and pass the request data to it. If you're just doing a simple single page script, loading some data, then your DB calls would follow.
If the request is invalid, then a simple header() call can notify the browser:
header('HTTP/1.0 404 Not Found');
And any data output would be the content of your 404 page.
I can't vouch for wordpress, one method I have used is to redirect 404's in .htaccess to index.php and then have that file sort by parsing:
$sub = $_SERVER['SERVER_NAME'];
$file = $_SERVER['REQUEST_URI'];
$sub can be used to mask non existant subdomains to a specific file. $file can be used in a switch or if clause to include / redirect based on file name.
Obviously, you need to make sure that the alias' are not actual files in your doc root.
Its called Routing(you can also check info about Front Controller pattern). You can write your own implementation by redirecting all your requests to single file using server settings and parse request in this file. You can also check, for example, Zend_Controller_Router docs and sources to understand how it works.
http://framework.zend.com/manual/en/zend.controller.router.html
http://framework.zend.com/manual/en/zend.controller.html

Categories