Codeigniter - getting 404 Not Found error - php

We have two hosting packages at godaddy. Our live website is working fine using following .htaccess file. Website is accessible without using index.php in url.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
We used same htaccess file and same code on other hosting that is also by godaddy. We just wanted to make production and development instances seperate. But on other hosting with same code and htaccess file, website shows 404 page not found error but website works fine with index.php in url.
We have same php version on both server. Just wants to know what could be the issue?

I also faced the similar problem when I move my 100% working code to dedicated Debian server. After 3 hours of research, i found out why htaccess file is not working.You need to allow overriding of htaccess in Apache Configuration.
Here are the steps.
Step 1:
Add this in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Step 2:
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3:
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
for www folder
Step 4:
Restart Apache (Command)
sudo /etc/init.d/apache2 restart
Before you make any changes in apache2.conf file make sure to make a backup copy of that file. If you are using Debian server you can avoid sudo command. I believe you know how to connect to your server using ssh client software like putty. Comment below if this method does not work for you.

Follow this
Create a new .htaccess file in your public_html/Project root
Paste the following code
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L,QSA]
Optional
If you can ssh into the server using command ssh username#ip_address on a command line either Linux terminal or Putty
The username is the one given to you by GoDaddy or you can use root
The ip_address would be the Public IP to your host you can find it in your hosting panel
Run the command sudo a2dissite 000-default
The command disables the default apache config that handles request
Goodluck!

You can use this. This works for me in godaddy hosting.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?/$1

try this one.. it's works for me..
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

I have faced the similar problem when I move on godaddy server.And It's work for me.
Add this code into your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
In config file:
$config['base_url'] = 'domain.com';
$config['index.php'] = '';
And Set AllowOverride to All in your httpd.conf
set the correct permission on the /cache folder, rather than just making the folder writable you need to make it reclusively writable.
I hope it will work for you.

Try to lowercase all your controllers. This fix the problem for me.

in index.php you echo "test" and run .then you can check whether it is called or not.
in config file enable error log .
Make your /application/logs folder writable.
In /application/config/config.php set. $config['log_threshold'] = 4;
check the log /application/logs folder

I have limited knowledge in server networking but this should give you a few ideas.
First ,you must know what is .htacess file is. you can check it here link
.htaccess is a configuration file for use on web servers running the
Apache Web Server software. When a .htaccess file is placed in a
directory which is in turn 'loaded via the Apache Web Server', then
the .htaccess file is detected and executed by the Apache Web Server
software. These .htaccess files can be used to alter the configuration
of the Apache Web Server software to enable/disable additional
functionality and features that the Apache Web Server software has to
offer. These facilities include basic redirect functionality, for
instance if a 404 file not found error occurs, or for more advanced
functions such as content password protection or image hot link
prevention.
Basically .htaccess is giving you permission to edit the configuration of Apache Web Server. You will gain permission using .htacess if mod_rewrite is enabled in Apache. I will not write what that 4 basic line in .htaccess can do in codeigniter because you can find it in google. this link can give you some nice basic info.
Now the main issue is .htaccess is not working in your other hosting. This is possible because a few reason :
For some reason, mod_rewrite is disabled in your Apache Web Server. If your hosting is VPS or DS it's possible you are didn't configuring it yet. Someone already ask how to enable mod_rewrite, you can go to this link. If you are using Shared Hosting, ask your hosting to enable mod.rewrite in your host. You can check if mod.rewrite enabled or not in your hosting following this link. Or you can simply check with <?php phpinfo(); ?> and check in "Loaded Modules" for mod_rewrite exist or not. Example in this image.
You are not using Apache as Web Server. There are others Web Server as NGINX and lighttpd. You should ask your server administrator what Web Server are you use currently. I dont have any experience using NGINX and lighttpd so i cannot give you any help in that. But as far as i know .htaccess code is different in APACHE, NGINX, and lighttpd. You can try to convert .htaccess apache code to nginx code in here or here. For converting .htaccess to lighttpd unfortunately, i cannot find any online converter but there are official guide here and here
Well for the complete tips sake i will write this. It's possible you are not configuring codeigniter to not use index.php. In config file, empty configuration for index_page. and give Base_url your domain name. $config['base_url'] = "yourdomainname.com"; $config['index_page'] = '';
I hope this clear your confussion why your .htaccess didnt work.

As you mentioned here, on localhost your code is working fine but when you moved it over live server it causes 404 error. There might be few cases you need to check:
Step 1: Check mod_rewrite is enabled or not in your Apache configuration
Step 2: Remove index.php in codeigniter config as #geordy suggested
You can check it by
<?php
echo 'Test'; die;
?>
in your root index.php. It ensures your all requests should go through this files else Codeigniter framework would not work properly.

Related

XAMPP Preview of PHP Redirects to Main Website

I recently had a third party contractor edit my website so you no longer see the ".php" at the end of each url. I am assuming he changed something with the .htaccess though I haven't been able to get a response from him since this job. Since then if I would like to preview my site for edits with XAMPP running the "localhost/filename" redirects me to my main online url. Is there anything I can change with with my xampp settings or .htaccess to allow me to view a preview for minor edits before uploading? I am not an expert in Apache or .htaccess files.
Thank you in advance.
EDIT Answer:
So this was not an .htaccess issue. It was a little more complicated than I thought. The easiest solution my colleague helped me through is as follows-
First I edited my host files using this tutorial: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/?fbclid=IwAR3d5pnrWgOdutlcrtThAzpIQSTteESgi6p6eWoPLaE_IPN-C1jZegLJBBI
In my host file I just put
127.0.0.1 yourmodel-local.com.
Second, I opened up the Apache Config file "httpd.conf" In that file under "ServerName localhost:80" line I added this:
<VirtualHost yourmodel-local.com:80>
DocumentRoot "C:/xampp/htdocs/yourmodel/"
ServerName yourmodel-local.com
So Now with XAMPP running I can use the yourmodel-local.com as my preview url.
From what you are saying, there's probably a RewriteRule or a Redirect instruction inside the .htaccess.
Normally those instructions are used to use a specific domain for example always use the https version of www.site.com.
You should look for lines similar to these:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
If you could provide that part of your .htaccess file's code I might be able to indicate what you would need to modify.
UPDATE
After reviewing the .htaccess file you submitted, the problem is related to lines that get automatically added to your .htaccess from your hosting company's server. The lines that contain strings like .well-known/acme-challenge are used for SSL certificate generation.
Your remote .htaccess has these three lines in the beginning:
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
The # is used for comment meaning those lines aren't being used.
So, I think the easiest solution for you in this case would be to leave the remote .htaccess file as it is, but edit the one you have locally to just contain this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Basically you'll have only this in your local .htaccess and leave the remote as it is, because it also has some PHP configuration lines, which will probably cause a 500 error on your local development environment. Be sure to not include the original # symbols.

Error with .htaccess mod-rewrite

I get error 500 Internal Server Error when using the below lines in .htaccess file in the main directory of the website.
RewriteEngine on
RewriteRule ^ar/?$ index.php?lan=ar [L]
RewriteRule ^en/?$ index.php?lan=en [L]
My intention is load website.com/index.php?lan=en when entering website.com/en. What could be the issue here?
Have you check if the mod_rewrite is enabled on your apache server ?
If you are on Windows, check the httpd.conf file inside the apache/conf directory on your web server.
On linux like Debian / Ubuntu, check if there is the file rewrite.so in /etc/apache2/mod-enabled/ directory. Otherwise, add the module with the command a2enmod rewrite && service apache2 restart
I am also not seeing an issue here might be some other rule or conflicting htaccess, but you can do that easily by,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?lan=$1 [QSA,L]

Apache mod_rewrite redirects links to localhost. How to make .htaccess to work with each site in local machine?

I've installed Xampp as a web server and want to make clean URLs for one of my projects. So I put these simple lines in .htaccess file in the root of this project:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also I made links in this project like this:
About Us
I expected to go to localhost/this_project/about/, but it does not work. I go to localhost/about/ and this is wrong. How can I solve this problem?
I also have put this line in the 2nd line of .htaccess file, but no change happened.
RewriteBase /this_project/
Attention: There are many directories in htdocs folder for each project.
You htaccess looks OK so I guess you are missing
AllowOverride All
In the VHOST <directory> block. If you server will fail after you add AllowOverride make sure you enabled mod_rewrite.

How to setup zend framework website without .htaccess

The setup of my .htaccess
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Guys i need your expertise on this one. Advice or idea would be great.
What i want to happen is that, i'll remove my .htaccess file and create a index.php to configure everything... Is it possible?
Can you please give some examples. Thank you in advance.
REFERENCE BY : http://www.armando.ws/2009/03/how-to-run-zend-framework-with-no-htaccess-file/
Application Specific Include file
Include conf/extra/httpd-zf.conf
This will allow Apache to add addition configuration settings once Apache is restarted and its a great place to isolate application specific Apache settings.
Now, go ahead an create the include file, “httpd-zf.conf” inside the APACHE_HOME/conf/extra/ directory and place the ReWrite rules which were originally in your .htaccess file into
it. Hit save, remove the .htaccess file, and restart Apache. You should see your Zend Framework application running.
Why do this?
Speed. Your application must always read the .htaccess file when someone visits the site. Removing this extra layer and placing it into the Web Server itself will reduce the amount of time a page loads.
the rewrite part is part of apache, and as such can only be configured in a way apache understands, so you can use .htaccess or the vhost config file directly. no other way.
the APPLICATION_ENV you could set in your index.php, but its there for a reason. so you dont have to change your code in different stagings/installation of your app.

CodeIgniter PHP Apache 500 Internal Server Error

I did a project in CodeIgniter. It's working fine on my localhost, but giving "500 Internal Server Error" on the remote server. This is my .htaccess file content:
RewriteEngine On
RewriteBase /ezgov
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Open httpd.conf generally located in Apache installation directory in windows
/apache/conf/httpd.conf
and
/etc/httpd/httpd.conf
in Unix based systems. httpd.conf is an Apache configuration file which stores various information about the server.
Search for the module mod_rewrite.so or (mod_rewrite.c in rare cases). mod_rewrite module is developed to rewrite the requested URLs on the fly. Most of the time you will find in commented state.
#LoadModule rewrite_module modules/mod_rewrite.*
Here # character represents that it is commented or disabled.
LoadModule rewrite_module modules/mod_rewrite.*
Remove # and restart the Apache Http Server using the command apache -k restart in windows or service httpd restart in unix systems. You also use XAMPP/Wamp UI to restart Apache in windows systems.
Next create .htaccess file in root directory where your CodeIgniter project is located and paste following code
# index file can be index.php, home.php, default.php etc.
DirectoryIndex index.php
# Rewrite engine
RewriteEngine On
# condition with escaping special chars
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Next Find CodeIgniter configuration file, generally located in it's configuration directory.
./application/config/config.php
Open the file and make $config['index_page'] value empty. which looks like this
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
Now the story is ended. Everything should work fine. You can find more information about httpd.config at Apache Module mod_rewrite. Hope this helps you. Thanks!!
Posting this, just in case it helps somebody...
Other answers to this question assume that you have access to apache's configuration files. But if you are on a shared hosting platform and do not have access to directories other than the ones where you are supposed to host your files: the trick was to force php to throw all errors even if apache could not.
Open "index.php" that resides in the root folder of your codeigniter installation;
/*switch to development environment*/
define('ENVIRONMENT', 'development');
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
error_reporting(E_ALL);
/*added line below*/
ini_set('display_errors', '1');
break;
......
Making the above change immediately started displaying what was wrong with the code and I was able to fix it and get it up and running.
Don't forget to set your codeigniter environment to "production" once you're done fixing it.
It's likely that the reason is explained in the error_log. Is there an error_log in the root directory of the web site? Is there a logs folder somewhere else? The reason will be detailed in there.
try this out :
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Probably you have problem with your htaccess file. It's helped me as well.
I solved this error.
I was given different database name, I have changed with following steps:
I have created database in server using cpanel
I have created a user
I have assigned the previously created database to user
I changed database settings with username and dbname etc.

Categories