CodeIgniter htaccess and URL rewrite issues - php

I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.
I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.
I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about
The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php
I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:
CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/
CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:
RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about
I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.
I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.
Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on StackOverflow on this subject but I couldn't find one that answered my question.
Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?

There are 3 steps to remove index.php.
Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
Make .htaccess file in your root directory using below code
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Enable the rewrite engine (if not already enabled)
i. First, initiate it with the following command:
a2enmod rewrite
ii. Edit the file /etc/apache2/sites-enabled/000-default
Change all AllowOverride None to AllowOverride All.
Note: In latest version you need to change in /etc/apache2/apache2.conf file
iii. Restart your server with the following command:
sudo /etc/init.d/apache2 restart

Your .htaccess is slightly off. Look at mine:
RewriteEngine On
RewriteBase /codeigniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /codeigniter/index.php?/$1 [L]
Notice "codeigniter" in two places.
after that, in your config:
base_url = "http://localhost/codeigniter"
index = ""
Change codeigniter to "ci" whereever appropriate

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dmizone_bkp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /dmizone_bkp/index.php?/$1 [L]
</IfModule>

This works for me
Move your .htaccess file to root folder (locate before application folder in my case)
RewriteEngine on
RewriteBase /yourProjectFolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Mine config.php looks like this (application/config/config.php)
$config['base_url'] = "";
$config['index_page'] = "index.php";
$config['uri_protocol'] = "AUTO";
Let me know if its working for you guys too ! Cheers !

I am using something like this - codeigniter-htaccess-file, its a good article to begin with.
leave the .htaccess file in CI root dir
make sure that mod_rewrite is on
check for typos (ie. controller file/class name)
in /application/config/config.php set $config['index_page'] = "";
in /application/config/routes.php set your default controller $route['default_controller']="home";
If you are running clean installation of CI (2.1.3) there isn't really much that could be wrong.
2 config files
controller
.htaccess
mod_rewrite
read
Codeigniter .htaccess
redirect-index-php-in-codeigniter

For those users of wamp server, follow the first 2 steps of #Raul Chipad's solution then:
Click the wamp icon (normally in green), go to "Apache" > "Apache modules" > "rewrite_module". The "rewrite_module" should be ticked! And then it's the way to go!

if not working
$config['uri_protocol'] = 'AUTO';
change it to
$config['uri_protocol'] = 'PATH_INFO';
if not working change it to
$config['uri_protocol'] = 'QUERY_STRING';
if use this
$config['uri_protocol'] = 'ORIG_PATH_INFO';
with redirect or header location to url not in htaccess will not work you must add the url in htaccess to work

I had similar issue on Linux Ubuntu 14.
After constant 403 error messages in the browser and reading all answers here I could not figure out what was wrong.
Then I have reached for apache's error log, and finally got a hint that was a bit unexpected but perfectly logical.
the codeIgniter main folder had no X permission on folder rendering everything else unreadable by web server.
chmod ugo+x yourCodeIgniterFolder
fixed first problem. Killed 403 errors.
But then I started getting error 500.
Culprit was a wrong settings line in .htaccess
Then I started getting php errors.
Culprit was same problem as main folder no X flag for 'others' permission for inner folders in application and system folders.
My two cents for this question: READ Apache Error.log.
But, also be aware of security issues. CodeIgniter suggests moving application and system folder out of web space therefore changing permissions shall be taken with a great care if these folders remain in web space.
Another thing worth mentioning here is that I have unzipped downloaded CodeIgniter archive directly to the web space. Folder permissions are likely created straight from the archive. As given they are secure, but folder access issue (on unix like systems) is not mentioned in CodeIgniter manual https://codeigniter.com/user_guide/installation/index.html and perhaps it shall be mentioned there with guidelines on how to relax security for CodeIgniter to work while keeping security tight for the production system (some security hints are already there in the manual as mentioned beforehand).

Just add this in the .htaccess file:
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]

By default the below sits in the application folder, move it like suggested into the root dir.
leave the .htaccess file in CI root dir

Open the application/config/config.php file and make the changes given below,
set your base url by replacing the value of $config['base_url'], as
$config['base_url'] = 'http://localhost/YOUR_PROJECT_DIR_NAME';
make the $config['index_page'] configuration to empty as $config['index_page'] = '';
Create new .htaccess file in project root folder and use the given settings,
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|images|js|css|uploads|favicon.png|favicon.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Restart the server, open the project and you'll be good to go.

Related

How should I configure redirects for multisite Codeigniter app?

I have multiple codeigniter sites setup like so:
/CI_Site1
/CI_site1/CI_Site2
/CI_site1/CI_Site3
Right now to use my the second and and third site I have to do it like so:
mydomain.com/CI_Site2/index.php/controller/function
If I don't put the index.php then it throws up a 404 in the root site(AKA CI_Site1).
How should I configure htaccess or apache site conf or CI config files such that I don't have to add index.php?
I think the first step should be to configure the routes in CI_site1 to not process requests for the other 2 but I don't know how. Here is hoping a Codeigniter pro can help me.
Thanks in advance.
EDIT: I know how to remove index.php from URLs with htaccess. But that is not the issue here. How do I prevent CI_site1 from processing requests that are for CI_site2 and CI_site3?
Just add below code in file and save it as .htaccess then upload it to Project folder
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Make sure your server mod_rewrite is on.
Did you try using routes?
$route['CI_Site1'] = '/CI_Site1';
$route['CI_Site2'] = '/CI_site1/CI_Site2';
$route['CI_Site3'] = '/CI_site1/CI_Site3';
I realized that no extra configuration was required. It is sufficient to use .htaccess to remove index.php. My issue was that apache was blocking .htaccess files in its default configuration. Editing apache2.conf to allow processing of htaccess files resolved my issue.

Can't open XAMPP & CodeIgniter Windows 10

In 2016/17 I had XAMPP working good but then I had one year rest and needed to reinstall Windows 10 and when I attempted to open the control panel it would not open.
I then decided to download the latest version of XAMPP and also CodeIgniter. I have transferred all of the files from the old version to the new version. I am able to open http://localhost/phpmyadmin and I have created a database with a table similar to that I had before. But if I enter http://localhost/intro I get the result of Object not found! - Error 404. I have checked routes.php and everything appears OK and I've also placed the Intro file (which is simple text without reference to the database) in the root folders of Controllers & Views, but get the same result. I have also checked config.php and changed $config['base_url'] = 'http://localhost/'; to $config['base_url'] = 'http://127.0.0.1:8080/localhost/'; but again it made no difference. I also changed the port number in the Control Panel to 8080 but again made no difference.
I guess by being able to open http://localhost/phpmyadmin that indicates Apache is working OK.
Can somebody tell me where else I can check?
If you are sure xampp is running perfectly then add .htaccess file in the main directory of the intro, add the following code into it:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
and try again. If you got any error let me help you.
My old system had a .htaccess file and there was not one in my new system. I copied & pasted that file and everything works good now.
My file contained the following.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Header add Access-Control-Allow-Origin "*"

CodeIgniter .htaccess rewrite not working on Live Server

I have a little problem with Code Igniter project, I have a .htaccess file to rewrite the index.php. It works great if I put my projects on Local Server. I am using WAMP Server. But now I have uploaded it on Live Server for example (godaddy,00webhost). And it is showing error :
404 Page Not Found.The page you requested was not found.
My .htaccess is here :
RewriteEngine on
RewriteBase /directory_name/
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favico​​n\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
and my config.php setting is here:
$config['base_url'] = 'http://example.com/directory/' ;
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
I have tried many solutions on web but not enough lucky to find one who helps me. I am stuck since 3 days. Anyone Help me.
If you are trying to host the website in a subfolder, make sure you enter the correct info in your htaccess and config:
Change RewriteBase /directory_name/in htaccess to RewriteBase /ci_foam/
Change $config['base_url'] = 'http://example.com/directory/' in your config to $config['base_url'] = 'http://php0713061.herobo.com/ci_foam/'
You always need to make sure these values matches your current server setup.

Removing index.php from Code Igniter

I've done a search for my question but have yet seen one similar to mine.
I have a newly installed CI on my server, for security reason I have shifted the "application" and "system" folder outside of public_html and into a folder called "app". I tried a lot of methods that the forums were using but none seem to help.
Below is my folder structure, would like to know what to type for the .htaccess and where does this .htaccess go to?
/
app (system and application folder for CI resides here)
public_html (index.php - the one which defines ENVIRONMENT, styles, other htmls and my current .htaccess resides here)
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Please help! Thank you for your time!
EDIT
Currently my url is something like this
xxx.xxx.xxx.xxx/~app/index.php/user/check/id
(it's on a shared hosting but not linked to domain yet and hence the ip address and partition in the url.)
I want to achieve something like
xxx.xxx.xxx.xxx/~app/user/check/id
and it should work when I link up my domain. (e.g. www.mydomain.com/user/check/id)
Alright, I think I've solved the problem.
This has most probably got to do with the 'unique' url that I have. I assume if it is a normal domain name it shouldn't have so much problems.
My url > xxx.xxx.xxx.xxx/~app/index.php/user/check/id
My directory structure remains the same >
/app (application and system directories goes here)
/public_html (index.php and .htaccess goes here)
.htaccess code as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~app/ << NOTE the addition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In config.php, set
$config['base_url'] = 'http://xxx.xxx.xxx.xxx/~app/';
$config['index_page'] = '';
That's all!

Codeigniter can only access default route controller

I suspect this may be a webserver configuration change that has taken place, and my hosting provider hasn't told anyone, but anyway...
I have a Codeigniter instance set up with the following directory structure:
/root
/codeigniter
/app
/sys
/www
/sandbox
/ci
/js
/css
/img
.htaccess
index.php
Therefore in index.php, I have the following variables:
$application_folder = '../../../codeigniter/app';
$system_path = '../../../codeigniter/sys';
These are relative definitions due to it being a shared hosting provider, however it does appear to be working that way so I don't think this is the issue (in WAMP i have those folder paths explicitly defined, e.g. c:/wamp/ci/application)
In config.php, I have the following variables set:
$config['base_url'] = 'http://******.co.uk/sandbox/ci/';
...
$config['index_page'] = '';
...
$config['uri_protocol'] = 'AUTO';
and my .htaccess file in www/sandbox/ci is set like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Up until a few days ago, this all worked fine, however, I replaced a fair few files just before Christmas, and may have accidentally overwritten something without realising it. This setup works fine on my WAMP server, but fails on my live server If I attempt to access any function in any controller that isn't the one defined in routes.php.
My routes.php looks like this:
$route['default_controller'] = "home_controller";
$route['404_override'] = '';
home_controller appears fine, however if I attempt to do something like login (which is processed via a form POST to auth_controller/login), it fails with 404. I do not have any additional functions in home_controller.
I get nothing interesting from setting the logging threshold to 4, and ChromePHP just suggests that it cannot find a path to the controllers.
mod_rewrite seems to be working, because if I add anything plaintext to the .htaccess file, I get a server error 500, and furthermore the index.php rewrite is working. I also have rewrite_short_tags set to TRUE.
I have tried all of the uri_protocol types in the config.php, and none seem to make any difference, so I feel like if it is something i've done, i'm either missing something from routes.php, or something missing from my .htaccess.
Any thoughts would be appreciated. Like I said, this entire setup works on my WAMP server without issue.
I think the problem is in your htaccess. Try this htaccess for a moment..
<IfModule mod_rewrite.c>
RewriteEngine On
# Set the rewritebase to your CI installation folder
RewriteBase /sandbox/ci/
# Send everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I hope this helps you out.

Categories