CodeIgniter Calling a folder/index.php from controller - php

I am currently using CodeIgniter 2.2 and I am getting the error message whenever I try running my test controller:
Unable to load the requested file: views/test/index.php
I want my controller to redirect me to index.php inside test folder under the views directory.
Inside my controller, I have:
<?php
class testControl extends CI_Controller
{
public function index() {
$view_folder='views/test/';
$data = '';
$this->load->view($view_folder.'index', $data);
}
}
In my application folder:
----controllers
---testControl
----views
---test
--index.php
Here is my .htaccess which I putted inside the root directory (along with the default index file).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
#RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
#RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
###
# 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]
# 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>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Am I missing anything? (Noted: My default welcome_page is working fine)

try something like this-
public function index() {
$view_folder='test/';
$data = '';
$this->load->view($view_folder.'index', $data);
}

Related

htaccess file dont work with special character

i have my .htaccess copied from codeigniter ,
i have a page which it should have a href
<a href="/notification/<?= rawurlencode(urlencode($tab['href']))?>">
htaccess will get the request correctly if i remove the special characters from the href
the htaccess file:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
ServerSignature Off
With codeigniter you shouldn't be passing URL segments with special characters. Those filters are there for security reasons. Instead, pass them as GET variables.
<a href="/notification?tab=<?=rawurlencode(urlencode($tab['href']))?>">
.. and in your controller
class Notification extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$tab = $this->input->get('tab');
}

Ignore the empty folder (403) and pass the URI to the Laravel controller with .htaccess

I have a project with Laravel and inside the public folder (httpdocs) I have a folder /en/news, all works fine with the route /en/news but when I try to go to the /en I have a 403 error because it is a empty folder, the real problem there is that /en is a route in my Laravel project with the homepage of the web, so first executes de .htaccess and throw the 403.
Is there any way to ignore this error for this specific folder/route (/en) or something like that?
My .htaccess is the tipical Laravel .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The solution:
Add before the # Send Requests To Front Controller... the next rewrite:
RewriteRule ^en/$ index.php [L]
This send the request to the index.php ignoring the checks for this specific route.

On godaddy server frontend not working in CodeIgniter

Backend is working fine but front end only default controller working if i call default controller writing name of controller and function then its not working
(giving 404 error "indialive_today/home/ was not found on this server.")
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/indialive_today
#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 ^(.*)$ admin/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>
routes.php
$usertype=explode("/",$_SERVER["REQUEST_URI"]);
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
if($usertype[3] == ''){ $route['default_controller'] = "home/index"; }
else if($usertype[3] != ''){ foreach( $result as $row ){$route[$row->slug] = 'news_detail'; }
}
try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
From post : https://stackoverflow.com/questions/17330398/codeigniter-and-godaddy-default-controller-issues
In your file, you have:
RewriteRule ^(.*)$ index.php?/$1 [L]
See the question mark? That means you are passing the request to the query string (QUERY_STRING). However, you are checking for a request URI (REQUEST_URI).
So, first, you need to change that line to:
RewriteRule ^(.*)$ index.php/$1 [L]
An even better option would be to do this:
RewriteRule ^ index.php [L]
You also need to add a forward slash to your RewriteBase:
RewriteBase /thinksoft/demo/indialive_today/
After some optimisations, your file (which should exist in the /thinksoft/demo/indialive_today/ directory) should look like this:
<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
# If you are using Apache 2.4.4 and later you can uncomment
# the line below, which is a decent alternative for using
# mod_rewrite
# FallbackResource /thinksoft/demo/indialive_today/index.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /thinksoft/demo/indialive_today/
# 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 ^(.*)$ admin/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]
# or use the following as a replacement to the rule above:
# RewriteRule ^ index.php [L]
</IfModule>
Note: I have also added the FallbackResource option (commented out), which can be quite useful.
Also make sure that your CodeIgniter configuration is set to use REQUEST_URI and not QUERY_STRING.

Code Igniter gives 404 when accessing pages without index.php

I'm getting 404 errors when accessing via:
http://www1.example.me/z/account/login
Although these work fine:
http://www1.example.me/z/index.php/account/login
http://www1.example.me/z/
.htaccess - got it from this website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(home(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|www1) [NC]
RewriteRule ^(.*)$ http://www1.sherzod.me/z/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://www1.sherzod.me/z/$1 [L,R=301]
###
# 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]
# 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>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Configs:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$route['default_controller'] = "home";
UPDATE:
Getting this in error_log:
[Wed Dec 28 17:54:30 2011] [notice] cannot use a full URL in a 401 ErrorDocument directive --- ignoring!
Never mind, fixed it.
Should have been:
RewriteRule ^(.*)$ z/index.php/$1 [L]
instead of
RewriteRule ^(.*)$ index.php/$1 [L]
because the code is inside of z folder. Thanks for visiting. :)
Whilst you have 'solved' your problem in your question, your implementation is actually incorrect.
To correctly implement this you should change the RewriteBase directive rather than pointing your rules specifically at a folder.
In your case
RewriteBase /z/
On a side note, rather than amending your question to say you've fixed it, you should post an answer to the same effect. This prevents the question appearing in unanswered feeds etc.
first you go to config file....
then set $config['index_page'] = ' ';
then create one file with .htacess extension and write below code in that and save in root directory
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Yes, you can easily remove the index.php from url from .htaccess, please check this url for more detail.

redirecting to codeigniter controller using htaccess

This is the url
http://rapidsurfing.net/visio/xip8yT
My site is developed in codeigniter. So when link this above url I want to go to one controller function. How can I do this using htaccess.
This is my httaccces code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^p\/(.*)$ page.php?q=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?admin/index/url=$1 [QSA,L]
</IfModule>
The url have value xip8yT.The admin is the controller name and index is the function in that controller.
But when I paste the url http://rapidsurfing.net/visio/xip8yT in address bar it load 404 not found error.
What is the problem or mistake in my code?
My default controller is admin.
First make sure you set up your default controller in routes.php file. A complete answer to your question is the way that I worked with CI. CodeIginter itself recommend to use a .htaccess for redirecting all of requests to your index page and your default controller and it's what you did. But I recommend to use this piece of code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourbasefoldername
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(yourdefaultcontrollername(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
# RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
# RewriteRule ^(.*)$ http://localhost/dfgamez/$1 [L,R=301]
# Enforce NO www
# RewriteCond %{HTTP_HOST} ^www [NC]
# RewriteRule ^(.*)$ http://localhost/dfgamez/$1 [L,R=301]
###
# 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]
# 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>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Simply you'll get every slashes after the site address to your index function parameters.
Peace Out!

Categories