Home » Php » php – Codeigniter – .htaccess giving 404 Page Not Found (Files are in a sub-folder)

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&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!

Answer:

Add the following in your .htaccess file

RewriteEngine on
RewriteCond $1 !^(index\.php|folername|uploaded|dist|images|plugins|js|css|cache|robots\.txt)
RewriteRule ^(.*)$ /projectfoldername/index.php/$1 [L]

Note: projectfoldername is your htdocs or project folder name

Answer:

Just try to change your last line as bellow and let me know please

RewriteRule .* index.php/$0 [PT,L]

Answer:

Try this Code :

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

Answer:

Try this code:

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

Answer:

try this code for .htaccess

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

also you need to change config > config.php

$config['base_url'] = '';

$config['index_page'] = '';

$config['uri_protocol'] = 'AUTO';

then your URL like this projectname/contrillerName/functionName/

Answer:

I suggest using Rewrite base /{your codeigniter folder}/ in .htaccess which works for me.

Answer:

In .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

In config.php (application/config)

$config['base_url'] = 'http://stackoverflow.com/'; # set this to prevent HOST Injectio.
$config['index_page'] = '';