I have a few sites set up on CI but latest one isn’t cooperating: browsing the domain doesn’t trigger home.php. I get “No input file specified”. I must be missing? (btw, when I put a test index.php file in root the echo code does render)
Here's what I've got set up -
//permissions:
//all subdirectories & file set to 755
//config.php:
$config[‘base_url’] = ‘http://example.com’;
$config[‘index_page’] = ‘home’;
//htaccess in root:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
//routes.php:
$route[‘default_controller’] = “home”;
$route[‘404_override’] = ‘’;
//home.php:
class Home extends CI_Controller {
public function index()
{
$this->load->library('session');
$this->load->view('main_view');
}
}
My settings seem to mirror those of my other the working CI sites but I’m definitely missing something. Would appreciate feedback on what else I should look for. Thanks
Since you're removing index.php from your URI, you don't need to specify an index_page in config.php. Setting $config['index_page'] = ''; should solve your problem. You should be fine leaving your $route['default_controller'] as it is, since CodeIgniter automatically defaults to the index method.
Your 'default_controller' config must be 'home/index'
Also be sure you placed the default config on the bottom of the file. In the CI routes files, always start from the most specific to the most general ...
Related
So I am doing a bit of research on this topic, and you would think there would be an answer but there isn't, or maybe I am looking in the wrong area.
Problem
When submitting a form my post variables are empty. I am passing them through properly, the form goes where it needs to go to, and the variables and form data appear properly and with values, in the headers. I have the form helper autoloaded in the config, so everything is how it should be.
my form opener looks like this:
<form action='/Forms/form_processor/' method='post' id='testForm'>
In my Forms controller looks like this:
define('BASEPATH') OR exit('No direct script access allowed');
class Forms extends CI_Controller{
public function form_processor(){
// get variables
$name = $this->input->post('name');
// ... do stuff with variable data
}
}
I would also note that with the use of the htaccess file my config variables are:
$config['base_url'] = 'http://example.com/';
$config['index_page'] = ''; // this is to keep index.php out of the URL
Possible Cause
When using codeigniter and sitting at the home page, you get this URL:
http://example.com
That is fine, until you go to the next page, you get this URL:
http://example.com/index.php/page
It looks ugly, so for aesthetic purposes and to keep uniformity I created an htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /example.com/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 http://example.com/
</IfModule>
and then by having this htaccess file at the base directory I get the URL to look like this:
http://example.com/page
Temp Fix and Questions Needing Answers
It was pointed out to me that because my $config['index_page'] variable is null that this is where the problems are stemming from. So I added the index.php to the beginning of my url in the form's action and it goes through.
Shouldn't the htaccess re-write handle this issue, and send it to where it is supposed to go where I have the index.php in the form's action URL or not?
Secondly, how would this affect the post variables? Because when I submit the form it still gets to where it needs to go to, it just shows null for all the variables, even though they are properly sent through the headers. This is where I am most confused.
If you need more information please let me know, but we are just trying to figure out why this is happening over here, it's kind of awkward. Thank you in advance!
Your question seems complicated. I think there might be an issue with installation steps. Remove index.php from config, set base url (also in application/config.php), set the encryption key(also there).
Check what are the session settings - did you change something there.
Also simplify htaccess (put it where is your index.php)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Is this local or server issue?
And last - try to use form helper, does it change something, whats inside console? Show more of your controller. You can check post inside one function
class Forms extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function form_processor()
{
//check if $_POST
if ($this->input->post()) {
$name=$this->input->post('name');
//process and redirect
}
$this->load->view('forms/my_form');
}
}
Also if you use xss_filtering or csrf_protection(check that in config.php) you should definitely use form helper.
/forms/form_processor/' method='post' id='testForm'>
Use base URL in form action.
If not work so use index.php after base URL.
I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!
As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.
I am having trouble in routing my files. I need to access the new_account subfolder and the controllers inside it...
Here is the structure:
controllers
---new_account
---------step1.php
---------step2.php
---accounts.php
---pages.php
This is the content of my .htaccess file located outside application
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And here is my routes.php
$route['default_controller'] = 'pages/show_page/home';
$route['pages/(:any)'] = 'pages/show_page/$1';
$route['accounts'] = 'accounts';
$route['new_account/(:any)'] = "new_account";
$route['(:any)'] = 'pages/show_page/$1';
$route['404_override'] = '';
I also use $config['uri_protocol'] = 'REQUEST_URI';
My pages controller is working perfectly, even as direct links e.g. <?php echo base_url(); ?>home redirects to ../pages/home.php.
The accounts.php controller calls new_account/user_home.php that contains guidelines and a link to the actual form. The link is <?php base_url();>new_account/step1 and it does not work which should have because of $route['new_account/(:any)'] = "new_account";.
Whenever I click the link to new_account/step1, I get redirected to the main home page.
I really want to access app_name/new_account/step1 and the others in the directory...Did I miss something in these files or an integral step?
NOTE:
I also tried this subfolder routing extension by inserting MY_Router.php in the application/libraries folder but no changes happened. I then moved it into application/core but all I get is a server error.
Comment this line in your routes.php:
$route['new_account/(:any)'] = "new_account";
Codeigniter default routes should work for http://YOUR_SITE/new_account/step1 cause you have a controller file called step1.php inside the folder new_account. Also check that the name of the class of your controller inside step1.php is Step1
I am having issues with the urls. I took the .htaccess code from the user guide to remove the index.php from the url and I have removed it from the config as well. Now here is the problem. I have a controller named “main” and a function inside called “join” which simply display the view. Now if I go to http://localhost/myfolder it loads the index view just fine. However if I try to go to http://localhost/myfolder/main/join or http://localhost/myfolder/join it gives me a 404 error. But http://localhost/myfolder/index.php/main/join works however without loading any css from my header file, but the footer is still loaded. Which I am very confused about. How can I possibly fix it so it just works with main/join? I will use routing later to make it just /join however I need the css to load like it does with my “index” view but not with my “join” view. Also localhost/myfolder/main gives me a 404 error as well. All I did was take the code from the user guide and paste it in .htaccess Any help guys?
Question is little confusing but let me try to answer what I got.
Question 1. Make 'localhost/myfolder/main/join' working.
You mainly need to remove index.php. Do the following:
a. (Very important) Make sure apache's rewrite module is enabled. IF you are using wamp, go to 'Apache'>'Apache modules' and make sure 'rewrite_module' is checked.
b. In code igniter, open file application>config>config.php. Search the line:
$config['index_page'] = 'index.php';
Remove index.php from there. Line must be
$config['index_page'] = '';
c. on myfolder, make a file .htaccess and add following code there:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Now URL 'localhost/myfolder/main/join' must be working.
Question 2: Load CSS
To load css, user following code under head section of your view:
<?php echo link_tag('style/style.css');?>
Make sure style/style.css is present in root folder; 'myfolder' in your case.
Hope this helps,
Kapil.
I know this has been asked a bunch of times, but I can't make it work at all. I'm trying to remove the index.php and the controller name (I only have one controller) from the URL.
So far, I was able to remove the index.php, but I still can't remove the controller name
Here is my htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|static|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Here is the top part of my config file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = '/main/';
$config['index_page'] = '';
And I put this as my last line in my routes file:
$routes['([a-z\_]+)$'] = "main/$1";
I'm using BlueHost, and the site is an addon domain to that account. Not sure if that makes any difference. But as of now, I was able to remove the index.php, but I want to remove "main" as well.
Any help would be great!
Thanks,
I'm not where I can try this to see if it works, but what if you leave the .htaccess as you have it and use this in your routes file?
$routes['(:any)'] = "main/$1";
One of the things I see here that you can change are:
$config['base_url'] = 'http://your-site.com/';
And in your routes.php
$route['default_controller'] = 'main';