2

I have a little problem with codeigniter, I have a .htaccess for rewrite the index.php it works great if I put my projects on /var/www or if I make a virtual host for it.

But I want to use my user directory such as http://localhost/~userdir/myproject. If I use my user directory when I'm trying to do request to a controller like: http://localhost/~userdir/myproject/signup I get this error:

Not Found

The requested URL /index.php was not found on this server.

This is my current configuration:

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

application/config/config.php

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['encryption_key'] = 'myencryption_key';
//all remaining is the default config

This config works great on http://localhost/ and also on production enviroments with a domain like http://example.com/ but not on my user directory http://localhost/~userdir/project/

What I doing wrong? Any thoughts? Thanks in advance.

carloscarcamo
  • 331
  • 1
  • 7
  • 12

3 Answers3

4

Well, I found a solution in codeigniter forums, first I need to add RewriteBase line as follows:

RewriteBase /~userdir/myproject/

Then, remove the slash before index.php on last line.

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Someone said that

RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favico​​n\.ico)

is redundant, the following two lines of code cover it,

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

and all other real files/directories. They basically say "if the request is not for a REAL file, and the request is not for a REAL directory, pass the request to index.php. Presumably everything from the above line is a real file or directory, so it's not needed at all.

So, my final .htaccess is like this:

RewriteEngine on
RewriteBase /~userdir/myproject/   
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

It works well locally with http://localhost/~userdir/myproject also on production with http://example.com/~userdir/myproject

carloscarcamo
  • 331
  • 1
  • 7
  • 12
  • and dont forget to change the apache configuration as mentioned in http://stackoverflow.com/a/14807463/1537413 – Dika Mar 12 '17 at 04:03
0

You need to remove the first slash berfore index.php like this

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Dumitru
  • 116
  • 1
  • 8
  • That not work for me, I get this: The requested URL /home/userdir/public_html/project/index.php was not found on this server. – carloscarcamo Feb 01 '15 at 05:35
  • sorry, i did't see that you removed the index.php from config. try to put the $config['index_page'] = 'index.php'; – Dumitru Feb 01 '15 at 11:56
0

The Local Solution

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

The Online Solution RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/$1 [L]

Ashraf Talha
  • 51
  • 1
  • 5