codeigniter in ubuntu always shows "404 not found" - codeigniter

welcome page only shows. If I create new controller and view means it shows '404 Not found' .
Controller - Create.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Create extends CI_Controller {
public function index()
{
$this->load->view('create');
}
}
View - create.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome to CodeIgniter!</h1>
</body>
</html>
I have run like this in browser - "http://example.com/Create/". But it shows 404 Not Found. I gave permission to all files also.

Firstly it showing welcome page because default controller is welcome in application\config\routes.php file
$route['default_controller'] = "welcome";
Only default controller can be use without index.php.
so try this for other controllers "http://example.com/index.php/controllerName/functionName"
You can refer to : https://ellislab.com/codeigniter/user-guide/general/urls.html

Do you have these set?
$config['base_url'] = '';
$config['index_page'] = '';

I think it is an upper case issue. Linux is case sensitive.
The fix is that you make sure you capitalize all your controller file names i.e in the controller folder, instead of welcome.php change it to Welcome.php
I hope this helped.

You need to enable mod_rewrite and set the "AllowOverride" option to "All" in your apache2.conf file.
To enable the mod_rewrite you just need to enter to a terminal and type the folllowing command:
a2enmod rewrite (as root user)
To set the AllowOverride All in the apache2.conf file you need to open it and set the AllowOverride option from None to All in the section, like this:
<Directory /var/www/html/>
AllowOverride All
</Directory>
If it doesn't exist then write it.

Related

Redirect upload to public/upload

I want force website redirect from https://tutorials.originersmc.com/uploads/ to https://tutorials.originersmc.com/public/uploads/
I know is using .htaccess, but I don't know code.
Any answer to add that redirect?
Try this, it redirects the user to public/uploads when the folder /uploads is requested
RewriteEngine On
RewriteRule ^uploads$ https://tutorials.originersmc.com/public/uploads/ [R=301,L]
I'd add this line to the .htaccess file when your site supports relative links:
Redirect 301 /uploads /public/uploads
For absolute links use this:
Redirect 301 /uploads/ https://tutorials.originersmc.com/public/uploads/
When someone accesses /uploads he will be redirected to /public/uploads
Make sure to edit the .htaccess file with a proper text editor.
Route::redirect('/uploads', '/public/uploads', 301);
Does the same when defined in web.php

base url is getting calling an inner folder

When I use echo base_url() in some portions of the application, it works fine.
My base_url() is http://localhost:8080/appname/
But when I do this <?php echo base_url(); ?>data/profile/avatar-5.png
The url appears like http://localhost:8080/appname/admin/data/profile/avatar-5.png where admin is a folder. I guess its because from where it is called from. But yet the base_url() should actually give the right url. Any ideas?
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php:
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Also don't forget to set base_url in application/config/config.php
$config['base_url'] = "http://localhost:8080/appname/';
Then try
<?php echo base_url('data/profile/avatar-5.png'); ?>
If the base path is not working for you, you can try my solution which I usually use
Create an separate path for logo or image
$config['image_path'] = ' http://localhost:8080/appname/admin/data/profile/';
use that in you view controller as
echo $this->config->item('image_path').$imagename;

Codeigniter base_url() in whack?

I was trying to set up a basic log in form with a following code:
<?=form_open(base_url() . 'main/login'); ?>
However after submitting the form the url shows this:
example.com/main/http//example.com/http//example.com/main/login
So I guess in essence for some reason the base-url is printed twice before the controller/method declaration. If I clear the base url value in my config file then the application works normally. I am however curious on what could cause this. For additional information I am working on xampp with a virtualhost and I have mod-rewrite on with a .htaccess file located at the document root.
CodeIgniter automatically adds the base_url to the action of the form when you use the form helper.
For example, you can use:
<?=form_open('main/login'); ?>
which will produce:
http//example.com/main/login
And a correct URL! Pretty simple! :D
More information at:
http://codeigniter.com/user_guide/helpers/form_helper.html
The file config.php under application/config has the setting:
$config['base_url'] = '';
Give it the folder/directory path. For example:
$config['base_url'] = 'http://localhost/ci_test/';
Don't forget to mention the protocol (http://). Alternatively try the site_url() method instead of base_url() for form opening. Skip it if using the form_open() function:
<form action="<?php echo site_url('main/login'); ?>"> ... </form>
Or
<?php form_open('main/login'); ?>
For more help: http://codeigniter.com/user_guide/helpers/url_helper.html
Not sure about the .htaccess file you have used. But this might be the answer codeigniter: why is that when i echo base_url() in an href attribute of an anchor tag, it echoes twice
Try it by parameter:
<?=form_open(base_url('main/login')); ?>
or
<?=form_open site_url('main/login')); ?>
In order to append the prefix also
You can Use
<?php echo form_open(base_url(main/login)); ?>
You have to use "echo" rather than because it not works in some browsers....

place images/css/js in views folder (codeigniter)

I am developing a small project that has user and admin sides. In my controllers folder there are two folders admin and user. Similarly in my views folder there are two folders admin and user. Both admin and user ends have different templates. I have placed CSS/Images/JS in assets folder that is placed in root folder (parallel to system folder). I do not want to make two different folders in assets folder for admin and user. Instead what i want is to place respective css/js/images in views/admin and views/user folders. This way i can remove assets folder and all html/css/images etc will be in same folder and i will be able to make different themes for user side. Is it possible?? If yes how?? Please guide me in detail.
edit: I want to place admin.css in application/views/admin and user.css in application/views/users
anyone who want to place all css/js/images in views can try this.
for example you want to place css file in views folder you will give CSS file path like this
href="<? echo base_url() ?>application/views/assets/css/admin.css"
now you will get forbidden access error. This error is cause of .htaccess file in application folder. open .htaccess file and copy paste following.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
You are ready to go..
Load the URL helper in the controller.
Then in views you can use
<link rel="stylesheet" href="<?= base_url() ?>assets/css/admin.css" />
<link rel="stylesheet" href="<?= base_url() ?>assets/css/user.css" />
off course change the path to the css file as required.
You can have separate folders then in your controller construct, change $this->load->_ci_view_path = {theme folder}.
Example: I store the templates information in a database table so,
$this->db->where('published', 'Y')->limit(1);
$res = $this->db->get('templates')->result();
$this->load->_ci_view_path = $res[0]->template_folder;
add one folder any name e.g public and add .htaccess file and write allow from all
it means, in this folder your all files and all folder will not give error Access forbidden!
use it like this
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo base_url(); ?>application/public/js/javascript.js"></script>
etc

Prevent direct access of downloaded file in CI

How to prevent direct access to my download file using CodeIgniter.
Supposed I stored all of my file in
application/secretdirectory/file1.jpg
application/secretdirectory/file2.text
application/secretdirectory/file3.zip
usually, I create direct link to access these files.
<a href="application/secretdirectory/file3.zip" > download here </a>
I want to prevent it, I want my link will be
<a href="<? echo site_url() . "download/file3.zip" ?>" > download here </a>
can you help me ???
It's probably a good idea to pass it to a controller like this:
Class File extends Controller{
function download($file){
$data = file_get_contents('path/to/' . $file); // Read the file's contents
$name = $file;
force_download($name, $data);
}
}
Your link would be:
<?php echo anchor('file/download/' . $thefile, 'Download');?>
This way they'd never know which directory it came from.
I'm sure there is a library in CI to assist with this
basically you send headers for the correct MIME type, the content length (filesize()) and then output to the browser to download echo file_get_contents() may work)
If using Apache, you'll also want to deny showing directoy content.
<Files .*>
Order Deny,Allow
Deny From All
</Files>

Resources