【问题标题】:mod_rewrite for multiple application in codeigniter在 codeigniter 中用于多个应用程序的 mod_rewrite
【发布时间】:2012-07-04 01:40:12
【问题描述】:

我是 url re-writng 的新手,目前正面临在 codeigniter 中为多个应用程序重写 url 的问题。我已经用一些可能的答案扫描了整个 stackoverflow,但它仍然没有完全解决我的问题

我的根目录是这样的

  • -应用程序
  • --管理员
  • --前端

我正在寻找的是让我的用户使用http://www.mydomain.com/ 访问我的网站(前端)和我的管理员(后端)作为http://www.mydomain.com/admin/

我当前的 .htaccess 就是这样

<IfModule mod_rewrite.c>
    RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ administrator.php [L,QSA]

# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ administrator\.php/$1 [L,QSA]

# If the user types any site section, like "site/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]

    ErrorDocument 404 /index.php
</IfModule> 

来自CodeIgniter multi-application .htaccess problem的代码

我可以使用上面的 .htaccess 在前端获得我想要的东西,但我不能让它在我的后端工作。基本上,www.domain.com/admin 给出了 404 page not found 错误。

请就这个问题给我建议。另外,请指导我应该将这个 .htaccess 文件放在哪里?在根?在应用程序文件夹中?或分别在前端和管理员文件夹中。

非常感谢。

问候。

【问题讨论】:

  • 对不起,目录是这样的 -application --administrator --frontend -administrator.php -index.php

标签: php .htaccess codeigniter url-rewriting codeigniter-url


【解决方案1】:

为什么不在您的根目录中设置这样的内容。

RewriteEngine on
RewriteCond $1 !^(index\.php|update\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

这基本上会忽略任何目录,例如“admin”,以路由到前端应用程序。您必须设置您的 config.php 并从控制器中删除 index.php。

【讨论】:

    【解决方案2】:

    已经找到了解决此问题的方法。这不是一种非常聪明的方法,但至少有效。

    首先,我在根目录中创建了一个“admin”文件夹,其中包含应用程序 index.php 的副本 这有效地使我的目录看起来像这样。

        -admin
        --.htaccess
        --index.php
        -application
        --frontend
        --administrator
        -index.php
        -.htaccess
    

    对于这个解决方案,您需要在不同的位置有两个 .htaccess 文件。 对于根目录下的 .htaccess,它应该如下所示。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|update\.php|administrator\.php|assets|admin|robots\.txt|favicon\.ico)
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule> 
    

    对于 admin 文件夹中的 .htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
    
        RewriteRule ^/(.*)$ index.php/$1 [L]
    
            RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
    
    
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule> 
    

    虽然这适用于本地主机,但值得注意的是,它可能不适用于租用的服务器,因为您主机中的文件系统可能不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      相关资源
      最近更新 更多