.htaccess throws a 500 error - php

I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from codeigniter tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not 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>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}

If this is really part of the .htaccess file, it would cause your problem:
RewriteRule ^{.*)$ /index.php?/$1 [L]
^^^ typo?
Should be:
RewriteRule ^(.*)$ /index.php?/$1 [L]

I too had the same problem but my code was correct and i was using wamp server, then
I found rewrite module is not enabled
so went to apache/conf/httpd.conf and uncommented line
LoadModule rewrite_module modules/mod_rewrite.so
Now its working fine.

Related

how to change domain/controller/method/parameter to domain/parameter?

actually i just wanna make this only for 1 controller is UserController.php.
<?php
use \application\controllers\MainController;
class UserController extends MainController{
function __construct(){
$this->setsession();
$this->model('user');
}
public function index($username){
if (!isset($_SESSION['clogin'])){
$this->template1('index');
}else{
$query = $this->user->selectWhere(array('username'=>$username));
$data = $this->user->getResult($query);
$this->template2('user', $data);
}
}
}
?>
according to code above, to access user page i need to go to localhost/web/user/index/username i want change to localhost/web/username. I have try this in .htaccess
RewriteRule /([a-zA-Z0-9]+)/$ /user/index/$1
base on question here, but it's not working. this is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule /([a-zA-Z0-9]+)/$ /user/index/$1
</IfModule>

Codeigniter return 404 page not found

I'm developing web application using Codeigniter. So far I have managed to run the application on my localhost. But when I upload the content unto the web hosting, I can't get it to run.
This is what I've tested so far:
strangely, I can call the controller Welcome.php which is the sample controller from codeigniter, and showing well.
But I can't call any other controller which my default controller eventhough I have set it in routes.php, config.php etc.
Below is my configuration for these files:
routes.php
$route['default_controller'] = 'lokadok';
$route['404_override'] = '';
$route['upload/do_upload'] = 'upload/do_upload';
config.php
$config['base_url'] = (( isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])>0) ?
'http://' . $_SERVER['HTTP_HOST'] : 'http://www.lokadok.co.id' );
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '.html';
$config['charset'] = 'UTF-8';
and this is the .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)\.html$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
My default controller is this:
lokadok.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lokadok extends CI_Controller {
public function index() {
$data['is_login'] = $this->session->userdata(LOGGED_IN);
$specialty = $this->input->post('specialty', '');
$address = $this->input->post('address', '');
$doc_name = $this->input->post('doc_name', '');
if ($specialty == '' && $address == '' && $doc_name == '') {
$this->load->model('model_share');
$data['custom_css'] = array(
'style/custom/lokadok.css'
);
$data['sort_by'] = 1;
$data['pop_specialties'] = $this->model_share->get_specialties(true);
$data['specialties'] = $this->model_share->get_specialties();
$this->load->view('templates/header', $data);
$this->load->view('lokadok_view');
$this->load->view('templates/footer', $data);
} else {
redirect("search?specialty=$specialty&address=$address&doc_name=$doc_name&sort_by=1");
}
}
}
?>
I have been looking for the answer for awhile and still couldn't figure it out. So if anyone can spot the error, I would really appreciate it.
To check on the location site you can go to:
www.lokadok.co.id which I expect to call my default controller.
and try to call http://www.lokadok.co.id/welcome which is strangely running well.
Thank you all, and hope my explanation is clear.
I think the answer lies within your question :
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I think you need to enable mod_rewrite
UPDATED
Find your php.ini in
/etc/php.ini
Or here:
/etc/php/php.ini
/etc/php5/php.ini
Find the line with disable_functions, one of them likely being phpinfo. You'll need to remove phpinfo from the list of disabled functions in disabled_functions=
IF UBUNTU THEN
To enable the rewrite module, run "apache2 enable module rewrite" in terminal:
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart

500 error thrown due to .htaccess bug [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htaccess throws a 500 error
I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from CI tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
In the log there is a row that repeats itself in every attempt: [Thu
Dec 20 04:01:13 2012] [alert] [client 2.55.118.161]
/home/morro1980/data/www/knight-guard.org/.htaccess:
directive requires additional arguments.
The mod_rewrite is installed and working
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not 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>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}
The first <ifModule> is not closed, this:
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule>
has to be
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
#^
Make sure you have <IfModule></IfModule> matched in your .htaccess.

routing/rewrites/removing index.php: Only works for default_controller, but not for the rest of the views

Followed a tutorial to remove index.php from the URLs in Code Igniter.
It works now for my default_controller, but not for other pages(or views) properly.
I have one controller, Pages, and it has one method, View($page = 'home'), to load pages with other content based on the parameter passed.
If I type localhost/dev into URL - I land on my home page, which is correct.
If I type localhost/dev/aboutus - I receive 404. It only works if I type localhost/dev/pages/view/aboutus.
What I wanted to happen is by typing localhost/dev/aboutus it would show me the AboutUs view.
Routes.php
$route['default_controller'] = "pages/view";
$route['dev/(:any)'] = "dev/pages/view/$1";
$route['404_override'] = '';
Pages.php (controller)
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if (!file_exists('application/views/pages/' . $page . '.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/' . $page);
$this->load->view('templates/footer');
}
}
?>
.htaccess file (located in /dev/ folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
#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>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
This route $route['dev/(:any)'] = "dev/pages/view/$1"; is a little fishy because CodeIgniter routes should not include the project name (dev in your case). They begin on the controller level, not the project one.
The route you have written would imply that if a user typed http://localhost/dev/dev/something (yes, two dev's) they would be routed to a controller dev.php and into a function with prototype: function('view', 'something'){}
Why so complicated? You should make all of your main page names into controllers. Routes should only be touched for special cases.
Create a file aboutus.php in application/controllers with contents:
<?php
class Aboutus extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
"I'm in the about controller!";
//$this->load->view("some_view") ;
}
}
You can access it with http://localhost/dev/aboutus
In your config.php leave it blank if you haven't
$config['index.php'] = '';

Codeigniter - cannot access controller while I have a default controller set

I have two controllers in the same folder. One is named criteria, and one is named reviewApprove. Their code is identical (other than the class name).
If I set criteria to the default controller in routes, then I am able to access it, but no matter what I cannot navigate to the reviewApprove controller.
If I don't set a default controller and navigate to index.php/reviewApprove, or index.php/criteria.. that will work just fine.
If I set reviewApprove to the default controller I can see it as well.
My htaccess file is set. I have compared my project to a few other code igniter projects and can't tell what I'm doing wrong.
Anyone have any ideas?
criteria controller
class Criteria extends APN_Controller {
public function __construct() {
parent::__construct();
$this->load->vars(array('nav' => array('','', 'current', '', '')));
}
public function index(){
$this->_set('title', "Certified Analytics");
$this->_set('js', "/resources/authenticated/js/coverage");
$this->_set('css', "/resources/authenticated/css/enroll");
$this->load->view('authenticated/header/header', $this->_data);
$this->load->view('authenticated/content/coverage');
$this->load->view('authenticated/footer/footer');
}
}
reviewApprove controller
class ReviewApprove extends APN_Controller {
public function __construct() {
parent::__construct();
$this->load->vars(array('nav' => array('','current', '', '', '')));
}
public function index(){
$this->_set('title', "Certified Analytics");
$this->_set('js', "/resources/authenticated/js/criteria");
$this->_set('css', "/resources/authenticated/css/enroll");
$this->load->view('authenticated/header/header', $this->_data);
$this->load->view('authenticated/content/reviewApprove');
$this->load->view('authenticated/footer/footer');
}
}
routes
$route['default_controller'] = "criteria";
htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /certifiedAnalytics/
#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>
Is anything else necessary?

Categories