【问题标题】:Codeigniter at subfolder and htaccess for inner redirection子文件夹中的 Codeigniter 和用于内部重定向的 htaccess
【发布时间】:2014-04-14 20:38:18
【问题描述】:

我在后端有一个带有自定义引擎的项目,我正在尝试使用我的 CodeIgniter 挂钩 /somewhere/ 的每个请求。所以我把我的 CI 放到/www/somewhere/,用新的 RewriteBase 配置了我的 CodeIgniter 的 .htaccess,所以它现在看起来像:

AddDefaultCharset UTF-8
Options All -Indexes

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

DirectoryIndex index.html index.php 

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]


</IfModule>

我的 CI 为这个项目创建静态 html 并将其存储在 /compiled/,所以我有这个结构

/www/
  /custom-engine/
  /img/
  /js/
  /somewhere/    <--- My CI's root
     /application/
     /system/
     /compiled/      <--- My storage for HTML
        my-file.html
        /my-compiled-folder/
        /one-more/
            /sub-compiled/
               index.html

一切正常。但。但是我需要在 URL 中打开没有/compiled/ 的编译文件。

作品:http://domain.com/somewhere/compiled/one-more/sub-compiled/

需要:http://domain.com/somewhere/one-more/sub-compiled/

我不需要重定向到我编译的文件夹/文件,只需打开它。所以我需要在我的 .htaccess 中添加一些东西。我还想继续访问我的 CI。例如,我有控制器helloworld,现在可以从http://domain.com/somewhere/helloworld/ 访问它(实际上我在这里有somewhere 的管理面板)。

所以.. 我想直接从我的/compiled/ 打开文件,但我还需要保存我的 CI。我应该在我的 .htaccess 中添加什么?

【问题讨论】:

    标签: .htaccess mod-rewrite codeigniter-2 subdirectory


    【解决方案1】:

    经过测试和工作。

    这个想法是验证REQUEST_URI而不是/system//application/,看看它是否存在于/compiled/文件夹中。

    RewriteEngine On
    RewriteBase /somewhere/
    
    RewriteCond %{REQUEST_URI} ^/somewhere/(system|application)/ [NC]
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
    RewriteCond %{REQUEST_URI} ^/somewhere/(.+)$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/somewhere/compiled/%1 -d [OR]
    RewriteCond %{DOCUMENT_ROOT}/somewhere/compiled/%1 -f
    RewriteRule . compiled/%1 [L]
    

    您似乎在 htaccess 中避免使用 /somewhere/。也许你也必须在我的代码中避免它(取决于服务器配置)

    【讨论】:

    • 谢谢!工作正常,而且我从你的回答中学到了一些东西。很高兴检查您的答案是否正确并给予赏金!再次感谢!
    • 我怎样才能拒绝直接访问我的/compiled/ www.domain.com/somewhere/compiled/test.html 但允许访问www.domain.com/somewhere/test.html
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多