【问题标题】:Codeigniter htaccess - controllers in subfolder not workingCodeigniter htaccess - 子文件夹中的控制器不起作用
【发布时间】:2012-05-11 07:21:36
【问题描述】:

我是 CI 新手,对重写规则了解不多。我能够从 stackoverflow 帖子中找到 htaccess 规则,它允许我从 URL 中删除 index.php。 ht访问规则如下

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /siebeluigen/

 #Removes access to the system folder by users.
 #Additionally this will allow you to create a System.php controller,
 #previously this would not have been possible.
 #'system' can be replaced if you have renamed your system folder.
 RewriteCond %{REQUEST_URI} ^system.*
 RewriteRule ^(.*)$ /index.php?/$1 [L]

 #When your application folder isn't in the system folder
 #This snippet prevents user access to the application folder
 #Submitted by: Fabdrol
 #Rename 'application' to your applications folder name.
 RewriteCond %{REQUEST_URI} ^application.* 
 RewriteRule ^(.*)$ /index.php?/$1 [L]

 #Checks to see if the user is attempting to access a valid file,
 #such as an image or css document, if this isn't true it sends the
 #request to index.php
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
 # If we don't have mod_rewrite installed, all 404's
 # can be sent to index.php, and everything works as normal.
 # Submitted by: ElliotHaughin

 ErrorDocument 404 /index.php
</IfModule>

但是,如果我将控制器直接移动到控制器内的子文件夹,这将不起作用。例如

如果我将应用程序控制器放在以下路径中

application/controllers/application.php

然后我可以从 URL 访问它

http://localhost/siebeluigen/application

但是如果我的目录结构是

application/controllers/app/application.php

然后我得到以下网址的错误

http://localhost/siebeluigen/app/application

但它的工作原理是这样的

http://localhost/siebeluigen/index.php/app/application

所以,我很确定 htaccess 中有一些东西我应该能够更改以使其正常工作。

请帮忙!!!

更新

我的 htaccess 文件位于应用程序文件夹而不是根目录中。将文件移动到 index.php 所在的文件夹解决了这个问题。我已经标记了有效的答案。 谢谢...

【问题讨论】:

  • 当您转到http://localhost/siebeluigen/app/application 时,您究竟遇到了什么错误?我测试了你的重定向代码,它有效。
  • 我收到未找到网络服务器错误“在此服务器上未找到请求的 URL /siebeluigen/app/application/”
  • 好吧,我自己不是 mod_redirect 专家,所以我现在不知道出了什么问题。也许您可以记录所有内容是如何被重定向的,并找出哪里出了问题(或在此处发布结果)。我读过another question,你可以登录:RewriteLog "/tmp/rewrite.log" RewriteLogLevel 9

标签: php .htaccess codeigniter mod-rewrite controller


【解决方案1】:

这是通常意味着重定向到 application.php 的条件

RewriteCond %{REQUEST_URI} ^application.* 

应用程序/应用程序不符合此条件。尝试将其更改为:

RewriteCond %{REQUEST_URI} ^app/application.* 

【讨论】:

    【解决方案2】:

    这是我在所有项目中使用的 htaccess 文件:

    Options -Indexes
    Options +FollowSymLinks
    
    # Set the default file for indexes
    DirectoryIndex index.php
    
    <IfModule mod_rewrite.c>
    
        # activate URL rewriting
        RewriteEngine on
    
        # do not rewrite links to the documentation, assets and public files
        RewriteCond $1 !^(images|assets|uploads|captcha)
    
        # do not rewrite for php files in the document root, robots.txt or the maintenance page
        RewriteCond $1 !^([^\..]+\.php|robots\.txt)
    
        # but rewrite everything else
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
    
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
    
        ErrorDocument 404 index.php
    
    </IfModule>  
    

    然后 - 还要确保在您的 config.php 文件中

      $config['index_page'] = '';
    

    编辑:你永远不想将某人指向你的应用程序文件夹——你总是将他们指向你的 index.php——一切都通过 Index.php——它会重新路由到需要的地方。

    【讨论】:

    • 我尝试使用您的 htaccess 文件.. 但它也不起作用。我收到此 URL localhost/siebeluigen/app/application 的网络服务器“未找到”错误,它仅适用于 localhost/siebeluigen/index.php/app/application
    • 首先 - 你确定你在本地服务器上启用了 mod_rewrite 吗?其次 - 你是把这个 htaccess 文件放在与 index.php 相同的文件夹中(它应该在哪里)
    • 谢谢 .. 问题是我的 .htaccess 文件位于应用程序文件夹而不是 index.php 文件夹中。将文件移动到 index.php 文件夹解决了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多