Search code examples
phpuricodeigniter-3

CodeIgniter URI routing with "#" as path parameter


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?


Solution

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