简体   繁体   English

从 codeigniter 4 中的 url 中删除 /public/,但重定向功能仍然抛出到 /public/

[英]Remove /public/ from url in codeigniter 4, but redirect function still throw to /public/

I changed my CI4 project configuration to remove /public/index.php from the url, and after that, the project can be accessed without that url.我更改了我的 CI4 项目配置以从 url 中删除 /public/index.php,之后,可以在没有该 url 的情况下访问该项目。 However, if I redirect the url using the redirect()->to('contoller) function, it still redirects to projectname/public/controller.但是,如果我使用 redirect()->to('contoller) 函数重定向 url,它仍然会重定向到 projectname/public/controller。

For example, suppose I access "projectname/admin", if not logged in, I should be redirected to "projectname/login", but instead of being redirected to that address, I was redirected to "projectname/public/login"例如,假设我访问“projectname/admin”,如果没有登录,我应该被重定向到“projectname/login”,但我没有被重定向到那个地址,而是被重定向到“projectname/public/login”

I did a test by disabling the element in the public folder, (renamed), and it changed to 404 after the redirect and says我通过禁用公用文件夹中的元素(重命名)进行了测试,并在重定向后更改为 404 并说

"Controller or its method is not found: \App\Controllers\Public::login" “找不到控制器或其方法:\App\Controllers\Public::login”

Is there anything that would need to be changed again regarding the removal of public from the url?关于从 url 中删除 public 是否需要再次更改?

By default, the index.php file will be included in your URLs.默认情况下,index.php 文件将包含在您的 URL 中。 If you want to remove it.如果你想删除它。

  1. Check mod_rewrite extension enabled in apache.检查在 apache 中启用的 mod_rewrite 扩展。

  2. update your .htaccess file with following code.使用以下代码更新您的 .htaccess 文件。

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

More information can be found here.更多信息可以在这里找到。

https://codeigniter.com/user_guide/general/urls.html https://codeigniter.com/user_guide/general/urls.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM