Codeigniter at subfolder and htaccess for inner re

2019-08-07 22:20发布

问题:

I have a project with custom engine at backend, I'm trying to hook every request at /somewhere/ with my CodeIgniter. So I put my CI to /www/somewhere/, configured my CodeIgniter's .htaccess with new RewriteBase, so it now looks like:

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>

My CI creates static html for this project and stores it at /compiled/, so I have this structure

/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

And everything works fine. But. But I need to open my compiled files without /compiled/ in URL.

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

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

I needn't redirect to my compiled folder/file, just open it. So I need add something to my .htaccess. Also I want keep access to my CI. For example I have controller helloworld and now it accessible from http://domain.com/somewhere/helloworld/ (in fact I have administration panel for this somewhere here).

So.. I want to open files directly from my /compiled/, but also I need to save my CI. What should I add to my .htaccess?

回答1:

Tested and working.

The idea is to verify REQUEST_URI other than /system/ or /application/ and see if it exists into /compiled/ folder.

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]

You seem to avoid /somewhere/ in your htaccess. Maybe you will have to avoid it in my code also (depends on server configuration)