2
votes

I'm using Ubuntu 13 with the following setup for a local codeigniter site.

Apache/2.4.6 (Ubuntu)
5.5.3-1ubuntu2.2 
'CI_VERSION', '2.1.2'

And URLs are no longer working without index.php. They used to work, but after upgrading from Ubuntu 12.x to 13.x and a few apache updates over the past year, the localhost sites no longer work right.

if I go to localhost/index.php/controllername/ it works but if I go to localhost/controllername/ it does not.

mod_rewrite is enabled.

CodeIgniter config has:

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // tried all available options here and 

Nothing worked

in the .conf file for the domain I have this:

<Directory />
  Options -Multiviews +FollowSymLinks
  AllowOverride All
</Directory>

and here's the .htaccess file commented lines are ones I tried that didn't work.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
#  RewriteCond $1 !^(index\.php|robots\.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
#  RewriteRule .* index.php/$0 [PT,L]
#    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

I’ve Googled and read everything I can find and tried everything I could find including several posts here on Stack Overflow, including the ones in the “Questions that may already have your answer.” Still nothing seemed to work. But like I said, this worked in the past, but only after multiple updates to the OS and Apache did I first notice it stop working.

I’ll be moving away from CodeIgniter with future projects, but these projects already existed. Baffled as to what could be the issue.

SOLUTION:

turns out it was not a codeigniter issue at all. It was an apache issue but not with the rewrite rules. in my apache2.conf I had to alter the block for /var/www/

Require all granted seems to have done the trick.

DirectoryIndex index.php index.html Options Indexes FollowSymLinks AllowOverride All Require all granted

just for good measure, I made the change here as well: Options FollowSymLinks AllowOverride All Require all granted

found on askubuntu https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working

4
Are you sure mod_rewrite is enabled? It’s not be default: sudo a2enmod rewrite. And then sudo service apache2 restart.Giacomo1968
did you set your variable in blank? in config file? $config['index_page'] = 'index.php'; should be like this $config['index_page'] = '';rm_beginners
look into this its a duplicate question stackoverflow.com/questions/20814773/…saurabh kamble
as stated, I saw many questions/answers on SO and none helped my situation. The rewrite rules in that question are exactly what I had but weren't working. Turns out it was not an issue with codeigniter at all, but with the apache config. I had to make some changes in apache2.conf under <Directory /var/www>keith73

4 Answers

1
votes

copy following code to .htaccess in your root folder

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

Options All -Indexes

This works fine for me to remove index.php in CodeIgniter.

0
votes

It doesn’t seem like CodeIgniter has a default .htaccess, so unclear where that came from. But for debugging purposes, I would recommend you do the following. First, here is your .htaccess all cleaned up:

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

Now replace your index.php with this. It just dumps the $_GET values passed via the .htaccess like so:

echo '<pre>';
print_r($_GET);
echo '</pre>';

Now with that in your .htaccess load the index page of the site/app/project in question. The output should be something like this:

Array
(
    [/controllername/here/] => 
)

Which appears to be correct. But again, you would know better than us.

The purpose of doing this is to assess whether the issue is either in Apache passing proper $_GET values to your CodeIgniter setup, which is one issue. Or whether the issue is within your CodeIgniter controller logic itself.

My gut tells me the issue is in the CodeIgniter logic in your codebase since the upgrade from Ubuntu 12.x to Ubuntu 13.x includes an upgraded version of PHP from version 5.3 in Ubuntu 12.x to version 5.5 in Ubuntu 13.x. I am using lots of code that works in PHP 5.3 and PHP 5.4 but breaks at times in PHP 5.5 since there are major changes in that codebase that will cause code to break if you don’t keep on top of non-depreciated functions & such.

0
votes

I would try something like:

RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]

If you know what GET variable the script is expecting for controllername in localhost/controllername, then in the rule replace myvariable with that variable name.

0
votes

Place the .htaccess file in the project folder like this:

htdocs/your_project/.htaccess

Try this new code for .htaccess:

RewriteEngine on 
RewriteBase /your-project-directory-name/ 
RewriteCond $1 !^(index.php|resources|robots.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L,QSA]