简体   繁体   中英

How to rewrite URL using Codeigniter route or htaccess file?

I have created a Home page controller with a Page method so that it can load inner page views. So I simply pass the Page name as an argument to Page method and it loads that specific view.

E.g. 
http://www.mywebsite.com/home/page/copyright

So here 1. home is controller 2. page is method 3. copyright is the parameter passed to page() method

So the home age loads simply using this URL

http://www.mywebsite.com/home/

and inner pages load using this url pattern

http://www.mywebsite.com/home/page/copyright

Now I want to load inner pages so that suppose I use this URL

http://www.mywebsite.com/copyright

it should load the page from

http://www.mywebsite.com/home/page/copyright

How can I do this using Codeigniter Route method or by using .htaccess file ?

Thanks in advance.

On your application/config/routes.php :

$route['copyright'] = 'home/page/copyright';

Even better:

$route['(:any)'] = 'home/page/$1';

So if you navigate to http://www.domain.com/copyright it routes to home/page/copyright
If you navifate to http://www.domains.com/anything it routes to home/page/anything

EDIT to catch any route

Although in this case you have to "catch" pages that you don t route like:

function page($section = ''){

 switch($section){
  case 'contact':  
   //display contact form
  break;

  case 'copyright':
  //display copyright
  break;

  default:
  //404 error
  break;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM