Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - .htaccess giving 404 Page Not Found (Files are in a sub-folder)

When I uploaded my file, I uploaded them in shared hosting. The thing is that the files and folders for my codeigniter are in one of those website name folders.

It gives me a 404 error when I visit the site.

My .htaccesss file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
     ErrorDocument 404 index.php
</IfModule>

I really don't know what to do. Any help is appreicated. Thanks.

Edit: When I edit the index.php file and just put something like echo 'Hey'; and delete the .htaccess file, it works fine.

like image 900
Taylor Avatar asked May 22 '15 02:05

Taylor


1 Answers

Based on your file structure:

file structure

The 404 error may be related to this part:

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

Which means: if the Apache module mod_rewrite.c is not being used or is not present, then all 404's are sent to index.php. If your codeigniter index.php is in your root "web" folder (public_html) then you should write ErrorDocument 404 /index.php instead. If it's in any sub-folders you need for example: /dyar.no/index.php and the remaining rules should also point to the same index.php. Your current .htaccess works well if you place it near your index.php (the same folder of the root folder of your website).

Besides, an .htaccess is not useful if you don't have meaningful conditions and rules there. The one you have is specifically for codeigniter, which has a system and an application folder.

You can have multiple .htaccess files in your other websites' folders (if you're using codeigniter in more than one of them, copy that .htaccess to those sub-folders e.g.: if dyar.no is using codeigniter copy that .htaccess to that folder).

My suggestion

I noticed that you are also using Wordpress and thus those rules could be changed accordingly... So delete your current root .htaccess file (from the public_html folder) and install BulletProof Security plugin in your wordpress site. Once you do, in the admin dashboard (BPS tab) you just need to click generate secure .htaccess (it generates a strong .htaccess according to your file structure and will cover most of the security issues). After that activate it on the root folder and on the admin folder and you should have all of it covered.

The generated .htaccess can also be a good reference to write other .htaccess you may need.

http://www.askapache.com/htaccess/htaccess.html is one of the best references I found to understand and write .htaccess and here are a few examples.

Hope it helps!

like image 72
Armfoot Avatar answered Oct 01 '22 18:10

Armfoot