这是我的带有SEO标题的URL
http://prdemos.com/Web/2015/assetsrecovered/index.php/pages/page?seo=ar-about-us
我想换
http://prdemos.com/Web/2015/assetsrecovered/ar-about-us
index.php/pages/page?seo=
应移除此部分。
首先设置config.php
$config['index_page'] = "";
和
$config['uri_protocol'] = 'PATH_INFO';
之后在routes.php
中
$route['default_controller'] = "welcome"; // Default Controller
$route['pages/([a-zA-Z-0-9.&_]+)'] = "pages/index/$1";
$route['404_override'] = '';
在我的header.php
中,我用slug(或SEO标题)调用了动态菜单
<?php
foreach($menu as $menu_item){
?>
<li>
<a href="<?php echo base_url();?>pages/<?php echo $menu_item['seo'];?>">
<?php echo $menu_item['name'];?>
</a>
</li>
<?php
}
?>
我的控制器是pages.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function index($seo='')
{
$this->load->model('MPages');
$data['seo']=$this->MPages->get_seo_by_page_slug($seo)->result();
// Loading Common Views
$this->load->view('header',$data);
$this->load->view('topbar',$data);
$this->load->view('mypage_view',$data);
}
}
最后我得到了这些网址
http://mysitelive/pages/ar-about-us
http://mysitelive/pages/ar-services
使用.htaccess
从url中删除index.php
,如下所示,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
要移除剩余零件,请使用routes.php
$route['{default_controller}/{default_method}/(:num)'] = "{original_controller}/{original_method}/$1";
删除index.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
这个.htaccess文件将允许在url中写入没有index.php的链接,但它看起来仍然是:http://prdemos.com/Web/2015/assetsrecovered/pages/page?seo=ar-about-us
您需要修改路由和控制器才能删除?seo=
。
在控制器中,您的操作应该是public function page($seo)
,类似于此,路由应该是$route['pages/page/(:any)'] = "pages/page/$1";
,然后在控制器操作函数中将您的$_GET['seo']
更改为$seo
。
之后,您可以访问您的页面,如:http://prdemos.com/Web/2015/assetsrecovered/pages/page/ar-about-us
如果要删除/pages/page
,则可以将其添加到routes.php
中:CCD_ 18。但是要注意这一点,因为路由文件中的错误位置会阻止其他路由在中工作