dongzu0742 2012-06-22 16:52
浏览 21
已采纳

从兄弟子目录访问CodeIgniter应用程序

I'm having a scenario where I had to deploy multiple directories for different languages. Development has initially centered on one of those languages, and just one part of the whole site is a small CI app.

I'm trying to avoid copying the whole app for the several other languages, and redirecting to it with an .htacces. The latter is working fine, but CI returns a 404 error when accessed from a URL different to the real one.

My best guess is that certain configuration files must exist with unique properties that configure the additional root URLs, but I don't know where to start (and Google didn't come up with a similar scenario).

File Structure:

public_html/
    lang1/
        app/    
            (the actual CI app)
        other static stuff...
    lang2/
        app/
            .htaccess (redirecting to /lang1/app/)
        other static stuff...
    lang3/
        ...

Additional info:

The $config['base_url'] is set to http://.../lang1/app/.

The .htaccess:

RewriteEngine on
RewriteRule ^(.*)$ /lang1/app/$1 [L]
  • 写回答

2条回答 默认 最新

  • dongpo1846 2012-06-25 14:52
    关注

    I was able to accomplish this by my own. For those who might find this question in the future, these are the steps to access your application from a different directory. Considering the scenario introduced in the question above, the following example will work for the deployment of of a ghost version of the app in the lang2 directory:

    1.- Copy only the index.php file from the root of your CI installation (from lang1/app/index.php) to the lang2/app/ directory.

    2.- Edit the following lines:

    $system_path = '../../lang1/app/system';
    
    
    $application_folder = '../../lang1/app/application';
    //Even if the documentation suggests the necessity of a _full path_, 
    //this works perfectly well for 2.1.0
    

    3.- Add any configuration you may want to have set explicitly for said subdirectory, these will be set to $this->config, replacing the values set in the config file in your base application:

    $assign_to_config['lang_version'] = 'lang2';
    

    4.- You have to set a proper base_url. In this case, we could reuse the lang_version config we just included. This way, we can forget about this line in the next languages we need to create.

    $assign_to_config['base_url'] = 
        'http://www.example.com/' . $assign_to_config['lang_version'] . '/app/';
    

    5.- Create an .htaccess file inside lang2/app to make sure that any static assets (js, css, images) accessed by the HTML are get from the actual assets folder inside the original app directory, like in lang1/app/assets:

    RewriteEngine on
    RewriteRule ^assets/(.*)$ /lang1/app/assets/$1 [L]
    

    6.- Add to this .htaccess your usual rule to keep your URLs friendly, but this time directing all traffic to the ghost copy of your app:

    RewriteCond $1 !^(index\.php|assets|robots\.txt)
    RewriteRule ^(.*)$ /lang2/app/index.php/$1 [L]
    

    7.- Grab a beer or any other beverage you drink to celebrate success, always respecting your local laws. Profit. Your controllers will have to read the config('lang_version') value to present the content in the proper language to the user. You may use the included language helper for that, or any other solution you prefer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式