如何在Codeigniter 4中删除url中的/index.php/?
开发学院2022-04-03 22:09:43
我希望网址像这样www.sample.com/controller而不是www.sample.com/index.php/controller,我要如何设置?
解决方案
打开app/Config/App.php,把public $indexPage = 'index.php'; 改成$indexPage = '';
然后需要配置web服务器:
对于apache服务器,需要编辑网站根目录的.htaccess,增加如下内容
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
对于nginx服务器,在站点对应的配置文件的/下面改为如下内容
location / { index index.html index.htm index.php; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } }
并加入如下内容:
location ~ \.php(.*)$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; #fastcgi_param ENVIROMENT 'production'; }
iis服务器,需要安装iis rewrite插件,粘贴如下内容到网站目录的web.config:
<rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(.*)$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:1}" /> </rule> </rules> </rewrite>
相关文章
- 功能强大的免费AI绘画工具:Stable Diffusion
- ChatGPT的中文平替:ChatGLM-6B
- 科普:为什么人工智能需要GPU而不是CPU?
- iis10安装URLRwrite组件
- 记一次cc攻击处理过程
- CodeIgniter4设置session变量为公共变量
- 在Codeigniter4中跟$this->db->last_query()功能一样的函数是什么?
- 如何为Spring Boot应用程序配置端口
- 安全狗Linux版命令行
- Codeigniter 4实体属性转换为未自动序列化的数组
- 在Codeigniter 4中使用分页时报错
- Mysql5.7创建用户并授权
- CodeIgniter 4设置会话变量$session=\\\\Config\\\\Services::session()为全局可用
- CodeIgniter4如何加载css和js文件?
- 如何调试Codeigniter 4项目?
- CodeIgniter 4中如何访问子目录的控制器?
- Whoops! We seem to have hit a snag. Please try again later
- 在CodeIgniter 4中redirect方法不能工作?
- 在CodeIgniter 4中,如何在保存到数据库后获取插入的id
- 如何在Codeigniter 4中删除url中的/index.php/?