0

I'm trying to adapt the .htaccess file in the root of my CodeIgniter installation so I don't have to include index.php in all the URLs leading to my controllers.

This is the content of my .htaccess file:

<IfModule authz_core_module>
    Require all granted
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

DirectoryIndex login.php

RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteCond $(REQUEST_FILENAME) !-f
RewriteCond $(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1

As you can see, it's the default .htaccess file with some rewrite rules for mod_rewrite and a DirectoryIndex directive appended to it. Mod_rewrite is enabled (I've checked this using apachectl -M).

Now, I'm running Apache 2.4.18 on Ubuntu 16.04 LTS and I am using CodeIgniter 3.1.6. The problem is that when I type in no URL I get redirected to CodeIgniters index.php instead of my wanted login.php. I'd rather get the DirectoryIndex directive working instead of transferring the contents of login.php into index.php for obvious reasons. My mod_rewrite rules are meant to route URLs to existing files "through" index.php without having to include index.php in their URL. This way, my URLs are cleaner but I can still use CodeIgniter's facilities.

My master Apache configuration obviously allows me to use .htaccess files (since CodeIgniter needs this too) and the URL rewrite directives work just fine. Why does the DirectoryIndex not seem to do anything no matter where I place it inside the file?

Vickel
  • 7,879
  • 6
  • 35
  • 56
Joshua Schroijen
  • 376
  • 6
  • 23

2 Answers2

0

Check out if you have AllowOverride instruction set to All -- it controls if .htaccess file has any power.

There are the answers on how to set it: How to Set AllowOverride all

0

I fixed the problem but not in an elegant way.

It appears to me that mod_rewrite just doesn't work (correctly) when you use it in a .htaccess file of a subdirectory of your DocumentRoot.

My CodeIgniter installation was in a subdirectory called "CodeIgniter" inside the DocumentRoot. I made two .htaccess files:

DocumentRoot/.htaccess:

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

DocumentRoot/CodeIgniter/.htaccess:

<IfModule authz_core_module>
    Require all granted
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>
Options FollowSymLinks
DirectoryIndex login.php

This seems to be doing the trick for me

Moral of the story: do all of your URL rewriting using mod_rewrite in the .htaccess located directly in your DocumentRoot

Joshua Schroijen
  • 376
  • 6
  • 23