Question:
My goal is to make an application written in Codeigniter run on the server in a subfolder and not on the main domain, example: www.dominio.com.br/foo
.
The error is about redirection , every request is being redirected to the home ( www.dominio.com.br
).
Content of the file www.dominio.com.br/foo/.htaccess
:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Excerpt from the 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'll let you know that I've already tried using RewriteBase that way…
RewriteEngine on
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
…but I wasn't successful either.
I also confess that I changed the .htaccess in every way and always redirects to home.
Thanks in advance!
Answer:
The correct path for RewriteRule
.
Note that you specified RewriteBase
, however, RewriteRule
still points to the root.
To solve this, 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 observations
-
The name
index.php
is unnecessary ifDirectoryIndex
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:
To 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