使用自定义htaccess mod重写时,有效路由上的Codeigniter 404

时间:2022-01-15 11:15:39

Im having an issue with codeignighter when using custom (somewhat strange) htaccess folder mapping. However, it should technically work, but it does not. Can someone help me?

我在使用自定义(有点奇怪)的htaccess文件夹映射时遇到了codeignighter的问题。但是,它应该在技术上有效,但事实并非如此。有人能帮我吗?


Scenario 1 (Works Fine):

场景1(工作正常):

Folder structure and key files

文件夹结构和密钥文件

/ is the website root
/.htaccess
/api/ <this is the CodeIgniter Root, with index.php and default CI .htaccess>

/.htaccess:

/.htaccess:

RewriteEngine On
RewriteRule ^api/(.*)$ /api/$1 [L]

accessing mydomain.com/api/admin/ for example gets me to my admin-controller/index action and loads the views fine.

访问mydomain.com/api/admin/例如让我进入我的admin-controller / index动作并加载视图。

var_dump on index.php for $_REQUEST shows

对于$ _REQUEST,index.php上的var_dump显示

array (size=1)
'admin/' => string '' (length=0)

Scenario 2 (Does not work):

场景2(不起作用):

Folder structure and key files

文件夹结构和密钥文件

/.htaccess
/current/.htaccess
/current/api/ <this is the CodeIgniter Root, with index.php and default CI .htaccess, this is the same folder/files/htaccess as above scenario>

/.htaccess:

/.htaccess:

RewriteEngine On
RewriteRule ^(.*)$ /current/$1 [L]

/current/.htaccess:

/current/.htaccess:

RewriteEngine On
RewriteRule ^api/(.*)$ /api/$1 [L]

accessing mydomain.com/api/admin/ for example gets me to the Codeigniter 404 Page/view.

例如,访问mydomain.com/api/admin/可以访问Codeigniter 404页面/视图。

var_dump on index.php for $_REQUEST shows (Same as Scenario 1)

对于$ _REQUEST,index.php上的var_dump显示(与场景1相同)

array (size=1)
'admin/' => string '' (length=0)

What is going on here? I know the htaccess rewrites are strange, but as far as what Codeigniter sees, the route is the same (admin/). But they both behave differently. Why? I tried everything i could think of, as far as i can see, codeigniter sees the right path as far as the Request Object goes, but the CI router somehow does not use it correctly.

这里发生了什么?我知道htaccess重写很奇怪,但就Codeigniter所见,路由是相同的(admin /)。但他们的行为都不一样。为什么?我尝试了所有我能想到的,就我所知,codeigniter看到了正确的路径,就像Request Object一样,但是CI路由器不知何故没有正确使用它。

Any help appreciated, let me know if you can think of anything i can try to help debug this further.

任何帮助表示感谢,如果您能想到任何我可以尝试帮助进一步调试的信息,请告诉我。

Note: the reason im using double htaccess is so that i can schedule different instances of CI depending on time/scenario (you can use rewrite condition to serve different folders instead of the "current" folder). Im currently not using this, so in scenario 2, the root only has the one "current" folder.

注意:我使用双htaccess的原因是我可以根据时间/场景安排不同的CI实例(您可以使用重写条件来提供不同的文件夹而不是“当前”文件夹)。我目前没有使用它,因此在方案2中,根只有一个“当前”文件夹。

2 个解决方案

#1


1  

Removing index.php from the URI at CodeIgniter can be a pain. Last week I was confronted to a similar case: I have my root web directory at /var/www/html and inside /ci my project... so inside /ci I have the following .htaccess:

从CodeIgniter的URI中删除index.php可能很痛苦。上周我遇到了类似的情况:我在/ var / www / html上有我的根网页目录,在/ ci我的项目里面...所以在/ ci我有以下内容.htaccess:

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

and in the apache2 config file I added the following:

在apache2配置文件中我添加了以下内容:

<Directory /var/www/html/ci/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Also check that in application/config/config.php you have:

还要检查application / config / config.php中是否有:

$config['uri_protocol'] = 'REQUEST_URI';

$ config ['uri_protocol'] ='REQUEST_URI';

Hope that helps...

希望有帮助......

#2


0  

You're redirecting the requests to a folder, while in fact you should be redirecting these to index.php file of CodeIgniter.

您正在将请求重定向到文件夹,而实际上您应该将这些请求重定向到CodeIgniter的index.php文件。

e.g:

例如:

RewriteRule ^api/(.*)$ /api/index.php?/$1 [L]

That is assuming you have 'index_page' set to empty string in your config file, If it's set to index.php, then when requesting something you should be including index.php in the url.

这假设您在配置文件中将'index_page'设置为空字符串,如果它设置为index.php,那么在请求某些内容时,您应该在url中包含index.php。

#1


1  

Removing index.php from the URI at CodeIgniter can be a pain. Last week I was confronted to a similar case: I have my root web directory at /var/www/html and inside /ci my project... so inside /ci I have the following .htaccess:

从CodeIgniter的URI中删除index.php可能很痛苦。上周我遇到了类似的情况:我在/ var / www / html上有我的根网页目录,在/ ci我的项目里面...所以在/ ci我有以下内容.htaccess:

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

and in the apache2 config file I added the following:

在apache2配置文件中我添加了以下内容:

<Directory /var/www/html/ci/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Also check that in application/config/config.php you have:

还要检查application / config / config.php中是否有:

$config['uri_protocol'] = 'REQUEST_URI';

$ config ['uri_protocol'] ='REQUEST_URI';

Hope that helps...

希望有帮助......

#2


0  

You're redirecting the requests to a folder, while in fact you should be redirecting these to index.php file of CodeIgniter.

您正在将请求重定向到文件夹,而实际上您应该将这些请求重定向到CodeIgniter的index.php文件。

e.g:

例如:

RewriteRule ^api/(.*)$ /api/index.php?/$1 [L]

That is assuming you have 'index_page' set to empty string in your config file, If it's set to index.php, then when requesting something you should be including index.php in the url.

这假设您在配置文件中将'index_page'设置为空字符串,如果它设置为index.php,那么在请求某些内容时,您应该在url中包含index.php。