如何为我的codeigniter网站写.htaccess文件

时间:2015-12-11 09:45:27

标签: php .htaccess codeigniter seo

这是我的SEO标题

的网址
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= 应删除此部分。

4 个答案:

答案 0 :(得分:1)

首先我设置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

答案 1 :(得分:0)

使用<li> <span style="display: inline-block; width: 30%;"> <label>labelname</label> </span> <span style="display: inline-block; width: 70%;"> <span>span value</span> </span> </li> 这样删除网址中的index.php

.htaccess

要删除剩余部分,请使用RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

routes.php

答案 2 :(得分:0)

删除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>

答案 3 :(得分:0)

<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$route['(:any)'] = "pages/page/$1。但是在预防措施中使用它,因为路由文件中的错误放置可能会阻止其他路由工作