Welcome Guest, Not a member yet? Register   Sign In
.htaccess when system/application folders outside doc root
#1

[eluser]a&w[/eluser]
Some will advocate an alternative folder structure to that provided with the default CI package. There are two basic suggestions:

1. make your application directory the same level as the default codeIgniter directory to make upgrades easier (just copy the entire package to that folder without worrying about affecting your application directory)

2. place application and system folders away from the public facing folder (where the index.php bootstrap file and perhaps your asset files are stored).

I struggled a bit trying to get the .htaccess to work for this folder structure, so I figured I'd share what I came up with in case it helps some other poor soul. I've also included the relevant bits from index.php and config.php files (I also adopted a few suggestions found on Michael Wales site recently).

Folder structure:
Code:
L public_html (for shared account)
    L myDomain
    .htaccess
        L codeIgniter
            L system
        L application
        L modules (optional)
        L public_html (redirect to this folder)
            index.php
            favicon.ico
            L assets
                L css
                L js

.htaccess
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

    #Define the part of the URL that will not change nor be used for rewriting.
    #In fact, this part will be removed before processing, and prepended
    #after processing. This is a good way to use subfolder-independent
    #rewrite rules.
    #For example, if your CodeIgniter index.php is placed in a virtual host
    #directory, like /tests/, set RewriteBase to /tests/.
    RewriteBase /public_html/

    #During the first parse, a variable REDIRECT_STATUS is set (= 200).
    #With these extra lines we can trap the second parse with a change nothing [L] instruction.
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule .* - [L]
    
    RewriteCond %{HTTP_HOST} ^myDomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.myDomain.com$
    RewriteRule ^/?$ /public_html/ [R,L]
    
    #Set Condition to meet for RewriteRule activation.
    #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]

    #If the request is for a file that exists already on the server, don't rewrite.
    #an exclamation mark ('!') is used to negate the meaning.
    #Test if the requested filename does not exist.
    RewriteCond %{REQUEST_FILENAME} !-f

    #Test if the requested directory does not exist.
    RewriteCond %{REQUEST_FILENAME} !-d

    #Do not rewrite access to images, css folders, and
    #the robots.txt file
    RewriteCond $1 !^(index\.php|images|robots\.txt|css\favicon\.ico)

#   RewriteCond %{REQUEST_URI} !^(.*)\.css$
#   RewriteCond %{REQUEST_URI} !^(.*)\.jpg$
#   RewriteCond %{REQUEST_URI} !^(.*)\.gif$

    #If RewriteCond conditions are met, this rule will be applied.
    #It inserts index.php before the requested URI.
    #The $1 represents the part of string enclosed by parentheses in left expression.
    #The [L] means that this rule is the last one if rule is applied (thus stopping rewriting).
#    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteRule ^(.*)$ /public_html/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 /public_html/index.php
</IfModule>

# disable directory browsing
Options All -Indexes

<FilesMatch "\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt)$">
    # Remove Etags inode (only use the modified time and size of the file)
    FileETag MTime Size
</FilesMatch>

<IfModule mod_expires.c>
    # must have mod_expires enabled in httpd.conf
    <FilesMatch "\.(gif|jpg|jpeg|png|swf|html?|txt)$">
        ExpiresActive On
        ExpiresDefault "access plus 10 years"
    </FilesMatch>
</IfModule>

index.php
Code:
&lt;?php

// Determine whether we're working on a local server or on the real server:
define('IS_PRODUCTION',
    (
        $_SERVER['SERVER_NAME'] != 'localhost'
    )
);

IS_PRODUCTION ? error_reporting(0) : error_reporting(E_ALL);
//Sets the default timezone used by all date/time functions in a script
date_default_timezone_set('America/New_York');

$system_folder = "../codeIgniter/system/";

$application_folder = "../../application/";

application/config/config.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$app_name = 'myDomain';

if (IS_PRODUCTION){
    $config['base_url'] = 'http://www.' . $app_name . '.com/';
} else {
    // local is slightly different to accomodate svn
    $config['base_url'] = 'http://localhost/' . $app_name . '/trunk/public_html/';
}

$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
$config['log_path'] = APPPATH . 'logs/';
$config['cache_path'] = APPPATH . 'cache/';
$config['sess_cookie_name'] = $app_name . '_session';//'ci_session'; // (give cookie a more unique name)

/* End of file config.php */
/* Location: /application/config/config.php */




Theme © iAndrew 2016 - Forum software by © MyBB