【问题标题】:Trouble re-routing URL's in CI在 CI 中重新路由 URL 时遇到问题
【发布时间】:2011-10-08 22:55:10
【问题描述】:

我无法将可变 URL 段重新路由到我设置的测试控制器。我希望案例 4 重定向到我的测试控制器。

案例 1(有效) 转到 URL http://localhost/2fb/index.php/redirect/test

//输出“测试”

Case2(不起作用)转到 URL http://localhost/2fb/redirect/test

//output "在此找不到请求的 URL /2fb/redirect/test 服务器。”

案例 3(有效) 转到 URL http://localhost/2fb/

//输出 -> 加载我的欢迎控制器。

Case4(不起作用)转到 URL http://localhost/2fb/abc

//output "在此找不到请求的 URL /2fb/redirect/test/abc 服务器。”

我的 routes.php 文件如下所示:

$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route["2fb/(:any)"] = "redirect/test";

redirect.php 控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Redirect extends CI_Controller {

    public function index()
    {    

    } 
    public function test()
    {
        echo "testing";                       
    } 

}

config.php:

$config['index_page'] = '';

.htaccess:

# Code Igniter Htaccess Rules
<IfModule mod_rewrite.c>
  RewriteEngine on
   # Rewrite URLs of the form 'x' to the form 'index.php/x'.
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

我相信我的 Apache 配置是正确的,因为它考虑了 .htaccess 文件的存在。

【问题讨论】:

  • 我修好了。两件事,我在 2fb 的根目录而不是在应用程序文件夹中编写和修改了 .htaccess 文件。我还在应用程序文件夹中有另一个 .htaccess 文件,其中写入了deny from all。在下面答案中的指令的帮助下,我删除了这个文件并用自定义文件替换了它。

标签: php apache .htaccess codeigniter routing


【解决方案1】:

也许尝试另一个 htaccess:

RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|css|img|images|js|scripts|system|uploads|robots\.txt|favicon\.ico|favicon\.png)
RewriteRule ^(.*)$ /index.php/$1 [L]

你还需要吗:

$route["2fb/(:any)"] = "redirect/test";

你能不能:

$route["2fb/(:any)"] = "redirect";

$route["2fb/(:any)"] = "redirect/$1";

【讨论】:

  • 当我重写.htaccess文件时,Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.我把它放到了index.php文件所在的2fb的根目录中。
【解决方案2】:

固定。

  • .htaccess 文件位于 CI 应用程序文件夹之外。

  • .htaccess 文件中有deny from all

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 2020-07-07
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 2021-01-13
    • 2021-02-14
    • 2020-10-03
    • 2015-10-01
    相关资源
    最近更新 更多