欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

php 路由 隐藏index,CI中路由与伪静态、隐藏index.php(十四)

发布时间:2023/12/10 php 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php 路由 隐藏index,CI中路由与伪静态、隐藏index.php(十四) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1,设置路由

默认控制器设置文件

application/config/routes.php$route['default_controller'] = 'welcome';

我们新建一个控制器文件

application/controllers/article.php<?php

class Article extends CI_Controller{

public function index(){

echo "这是一个Article的index方法";

}

}

访问:index.php/article/index

在写一个方法public function show($id){

echo '这是文章'.$id;

}

访问:index.php/article/show/4

或者index.php/article/show/4.html

我们现在路由配置文件里面写一个正则

application/config/routes.php//index.php/news/201654/4.html

$route['news/[\d]{6}/([\d]+)\.html']='article/show/$1';

2,去掉index.php

前提是需要确定Apache开启rewrite模块

wamp/bin/apache/apache2.4.9/conf/httpd.conf

打开注释LoadModule rewrite_module modules/mod_rewrite.so

让后修改项目配置文件.htaccess文件,添加代码

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

这样就可以去掉index.php

如果访问错误,再去application/config/config.php文件里$config['index_page'] = 'index.php';//把index.php去掉

总结

以上是生活随笔为你收集整理的php 路由 隐藏index,CI中路由与伪静态、隐藏index.php(十四)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。