Codeigniter and . htaccess in subfolder on server

Asked

Viewed 2,235 times

3

My goal is to make an application written in Codeigniter run on the server in a subfolder and not in the main domain, example: www.dominio.com.br/foo.

The error is about redirecting, every request is being redirected to the home (www.dominio.com.br).

Contents of the archive www.dominio.com.br/foo/.htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Excerpt from the archive www.dominio.com.br/foo/application/config/config.php:

$config['base_url'] = 'http://www.dominio.com.br/foo/';
...
$config['index_page'] = '/index.php';

I would say that I have already tried to use the Rewritebase thus...

RewriteEngine on
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

...but I didn’t succeed.

I confess that I changed the . htaccess of all that is way and always redirects to the home.

Thank you in advance!

  • That one / at the beginning of his RewriteRule (/index php.), is not directing to the root of your server? The .htaccess original of Codeigniter doesn’t have this bar.

  • Without the / tb does not work! Obg.

4 answers

1


We have yet to define the correct path to RewriteRule.

Note that you specified RewriteBase, however, RewriteRule still pointing to the root.

To settle, do it: RewriteRule ^(.*)$ /foo/index.php/$1 [L]

Full example:

RewriteEngine on
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /foo/index.php/$1 [L]

Some remarks

  1. The name index.php it is unnecessary if DirectoryIndex is already pointing to the same.

  2. When applying to a root directory, it is good practice to specify the basis RewriteBase /.

Alternative

Alternatively, a more streamlined way for HTACCESS rules:

For root/root directory

RewriteEngine on
RewriteBase /
Options FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.+) /
DirectoryIndex index.php

To a subfolder

RewriteEngine on
RewriteBase /foo/
Options FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.+) /foo/
DirectoryIndex index.php

0

I usually leave mine application/config/config.php thus:

$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 thus:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and I don’t usually have any problem leaving in subfolders as in your case you need, which by the way I always leave like this to do a test.

0

If you put your CI with the default structure inside the FOO folder there is no reason why it doesn’t work. What it looks like is that your basis for writing is in another folder. Try this pattern below; inside your folder in the application hierarchy.

'<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>'

That should work!

0

Some considerations that might help you:

  1. If you use the MVC approach, especially the VC, having a controller foo and in it the function index() calling the desired view, would work the way you want it to.

  2. 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
    
  3. View calls can also be made inside folders: Ex: $this->load->view('pasta/arquivoPhp');

Note: I usually leave the $config['base_url'] = ''; and $config['index_page'] = ''; empty or in the $config['base_url'] = 'http://www.dominio.com.br/foo'; without the last bar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.