Url hide using codeigniter

2019-01-20 20:41发布

问题:

I am using codeigniter... I want a clean URL from below URL

  http://localhost:8080/rtvnews/index.php/home/videonews?id=67598/newstitle 

here home => controller, videonews => function and ?id=6586565 is a url string.

I want to remove /index.php/home/videonews?id=67598 and replace with /news/

Below Final url I need to get

  http://localhost:8080/rtvnews/news/newstitle 

回答1:

For hiding index.php from url use following htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

More on htaccess

URI Routing

Considering newstitle as unique:

Go to your route.php file (application/config/routes.php) . Add new route rule as below

$route['rtvnews/news/(:any)'] = 'home/videonews/$1';

in your view file

<a href="<?php echo base_url()."rtvnews/new/".$newstitle; ?>" > News Title</a>

so your url became as below

http://localhost:8080/rtvnews/news/uniquenewstitle  

Your request go to home/vidonews function where you can get your newstitle as parameter.

In your controller.php function will be like

function vidonews($newsTitle){}

Considering id as unique :

Add new route rule as below

$route['rtvnews/news/(:num)'] = 'home/videonews/$1';

In view.php file

<a href="<?php echo base_url()."rtvnews/new/".$newsId; ?>" > News Title</a>

so your url became as below

http://localhost:8080/rtvnews/news/newsId

Now your request go to home/vidonews function where you can get your newsId as parameter. In your controller.php function will be like

function vidonews($newsId){}


回答2:

Follow the steps below

Step 1: .htaccess (the one which is at root folder)

Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Step 2: routes.php add the code below

$route['rtvnews/news/newstitle'] = 'Your controller/method']; //Just a syntax to change the route in codeigniter. You can change the url as per you want.