Question :
My goal is to make an application written in Codeigniter run on the server in a subfolder and not in the main domain, eg: www.dominio.com.br/foo
.
The error is about redirection , every request is being redirected to home ( www.dominio.com.br
).
Content of file www.dominio.com.br/foo/.htaccess
:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Excerpt from file www.dominio.com.br/foo/application/config/config.php
:
$config['base_url'] = 'http://www.dominio.com.br/foo/';
...
$config['index_page'] = '/index.php';
I’ve already tried using RewriteBase in this way …
RewriteEngine on
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
… but tb I was not successful.
I confess tb that I changed the .htaccess of everything that is form and always redirects to the home.
Thank you in advance!
Answer :
Failed to set correct path to RewriteRule
.
Note that you have specified RewriteBase
, but RewriteRule
continues to point to the root.
To resolve, do this: RewriteRule ^(.*)$ /foo/index.php/$1 [L]
Complete example:
RewriteEngine on
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /foo/index.php/$1 [L]
Some remarks
The name index.php
is unnecessary if DirectoryIndex
is already pointing to it.
When applying to a root directory, it is good practice to specify the base RewriteBase /
.
Alternative
Alternatively, a more simplified form for HTACCESS rules:
For root / root directory
RewriteEngine on
RewriteBase /
Options FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.+) /
DirectoryIndex index.php
For a subfolder
RewriteEngine on
RewriteBase /foo/
Options FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.+) /foo/
DirectoryIndex index.php
If you put your IC with the default structure inside the FOO folder you do not have why it does not work.
What it seems is that your basis for writing is in another folder.
Try this pattern below; inside your folder in the hierarchy of the application.
'<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>'
That should work!
Some considerations that might help you:
foo
controller and the index()
function calling the desired view would work the way you want.
CodeIgniter allows you to work with the routes of your url through the file routes.php
:
$route['bar'] = 'foo'; // rota foo para o controller bar
View calls can also be made within folders: Ex: $this->load->view('pasta/arquivoPhp')
;
Note: I usually leave $config['base_url'] = '';
and $config['index_page'] = '';
empty or in $config['base_url'] = 'http://www.dominio.com.br/foo';
without the last bar.
I usually leave my application/config/config.php
like this:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'].= "://" . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "");
$config['base_url'].= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['index_page'] = '';
and my .htaccesss
like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And I do not usually have any problem leaving it in subfolders as in your case you need it, which by the way always leave it like this to do a test.