upload project in web host and 404 page not found - codeigniter
i have problem with codeigniter 3 , the project work fine in localhost , but when i upload my project in host , i see
404 Page Not Found
The page you requested was not found.
i remove .httaccess file , remove all of my routes , but dont work.
i have two controller :
class test extends CI_Controller {}
class testen extends CI_Controller {}
my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,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>
edit:
my routes:
$route['default_controller'] = 'isogamsharghedonya';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route["en"] = "isogamsharghedonyaen";
$route["fa"] = "isogamsharghedonya";
$route["en"] = "isogamsharghedonyaen/index";
$route["en/isogam-sharghe-donya-service"] = "isogamsharghedonyaen/service";
$route["en/isogam-sharghe-donya-news"] = "isogamsharghedonyaen/news";
$route["en/isogam-sharghe-donya-tvc"] = "isogamsharghedonyaen/video";
$route["en/isogam-sharghe-donya-certificate"] = "isogamsharghedonyaen/certificate";
$route["en/isogam-sharghe-donya-honor"] = "isogamsharghedonyaen/honor";
$route["en/isogam-sharghe-donya-ceo"] = "isogamsharghedonyaen/aboutCeo";
$route["en/isogam-sharghe-donya-about-company"] = "isogamsharghedonyaen/aboutCompany";
$route["en/isogam-sharghe-donya-team"] = "isogamsharghedonyaen/aboutTeam";
$route["en/isogam-sharghe-donya-contact"] = "isogamsharghedonyaen/contact";
$route["en/send-message"] = "isogamsharghedonyaen/sendMail";
$route["en/insulation-orders"] = "isogamsharghedonyaen/order";
$route["en/new-insulation-orders"] = "isogamsharghedonyaen/newOrder";
$route["fa"] = "isogamsharghedonya/index";
$route["fa/" . rawurlencode("خدمات-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/service";
$route["fa/" . rawurlencode("اخبار-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/news";
$route["fa/" . rawurlencode("تیزر-های-تبلیغاتی-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/video";
$route["fa/" . rawurlencode("گواهینامه-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/certificate";
$route["fa/" . rawurlencode("افتخارات-و-جوایز-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/honor";
$route["fa/" . rawurlencode("مدیر-عامل-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutCeo";
$route["fa/" . rawurlencode("درباره-شرکت-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutCompany";
$route["fa/" . rawurlencode("تیم-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutTeam";
$route["fa/" . rawurlencode("تماس-با-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/contact";
$route["fa/" . rawurlencode("ارسال-پیام")] = "isogamsharghedonya/sendMail";
$route["fa/" . rawurlencode("سفارش-ایزوگام")] = "isogamsharghedonya/order";
$route["fa/" . rawurlencode("سفارش-جدید-ایزوگام")] = "isogamsharghedonya/newOrder";
require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db -> get('news');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subjectFA)));
$route["fa/news/" . $string] = "isogamsharghedonya/newsDetails/$row->id";
}
$query = $db -> get('news');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subjectEN)));
$route["en/news/" . $string] = "isogamsharghedonyaen/newsDetails/$row->id";
}
$query = $db -> get('product');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameFA)));
$route["fa/product/" . $string] = "isogamsharghedonya/productDetails/$row->id";
}
$query = $db -> get('product');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameEN)));
$route["en/product/" . $string] = "isogamsharghedonyaen/productDetails/$row->id";
}
$query = $db -> get('trailer');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameFA)));
$route["fa/video/" . $string] = "isogamsharghedonya/videoDetails/$row->id";
}
$query = $db -> get('trailer');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameEN)));
$route["en/video/" . $string] = "isogamsharghedonyaen/videoDetails/$row->id";
}
Your controller and model files must begin with a capital letter.
An example: controllers/Index.php, models/Index_model.php
code in CI 3 core for controllers load (/system/core/router.php):
protected function _set_default_controller()
{
//... blah-blah-blah ...
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
{
// This will trigger 404 later
return;
}
//...blah-blah-blah...
}
It work in localhost, because you system is Windows. Unix like systems is case sensitive in work with files.
As i reviewed your code the first thing that you have rectify is that the way you have declare your controller since it is a good practice to define your controller as class Test extends CI_Controller instead of class test extends CI_Controller as you have declared some time it might create error and the second thing is that you have to check your .htaccess file is uploaded to your root directory of the project and as well as you also check in your routes file whether you have written the appropriate routing rule for your declared controller as well as the is the method is present in your class which is defined in your routes for the same
Related
Redirecting function not redirecting
I try to redirect to a specific function in the same controller, but it doesn't redirect and only shows a white page (blank). Trying to redirect to some specific url, it's still not moving to the url. signin controller: public function index() { $this->generateToken(); $data = array( 'title' => 'MASUK', 'breadcrumb' => 'MASUK', 'menu' => 'MASUK', 'login' => 'active', 'captcha' => $this->refresh_captcha(), 'msg' => '', 'infoMSG' => '' ); if (isset($this->session->userdata['logged_in']['usr_email']) == "") { $this->template->load('template', 'signin', $data); } else { //var_dump(base_url()); // show the base url //redirect('https://www.facebook.com'); // not direct to specific url too redirect('Signin/logout'); } // else { // redirect('landing'); // } } public function logout() { print_r("mask"); //not print anything $this->load->library('session'); $this->session->unset_userdata('permission'); $this->session->unset_userdata('logged_in'); $this->session->unset_userdata('session'); $this->session->unset_userdata('userinfo'); $this->session->unset_userdata('api_loc'); $this->session->sess_destroy(); redirect('signin'); } .htaccess file: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
Try adding a ? at the end of index.php in your last rewrite rule. RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA] If that doesn't work, try changing your config.php 'uri_protocol' if it's set to AUTO. $config['uri_protocol'] = 'REQUEST_URI';
code igniter redirect page not working as expected
if(){ redirect('Login'); } I have a condition above that redirect the page to log in depending on some values. I can see in url that I get redirect because when I enter http://www.myapp.com it changes to http://www.myapp.com/Login Now In my route I have: $route['default_controller'] = 'welcome'; $route['Login'] = 'login'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; My Login controller looks like: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('session'); $this->load->model('Login_model'); } public function index(){ $data['title'] = 'GYM'; $this->load->view('login',$data); } } In my view I have But why Im getting Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 www.myapp.com Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7 I cant figure out where I got configuration wrong Update: change config file: $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].''; $config['index_page'] = ''; $config['uri_protocol'] = 'REQUEST_URI';//'AUTO'; $config['url_suffix'] = ''; $config['language'] = 'english'; $config['charset'] = 'UTF-8'; $config['enable_hooks'] = FALSE; $config['subclass_prefix'] = 'MY_'; $config['composer_autoload'] = FALSE; $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] and RewriteEngine on RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] accessing: http://www.myapp.com/index.php/Login Works but all the above does not. I not getting why
Try including below lines on the .htaccess at the root level of the codeigniter app folder : RewriteCond $1 !^(index\.php) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
change your value like following $route['login'] = 'Login'; $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/'; if not work, replace this code in htaccess RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Query string in codeigniter
After I remove index.php, I enable query string in codeigniter . But I have some trouble with redirect link. Detail , I have login form (login/index) and when login success redirect to "welcome/index" and save email in session . But when login success only load view of "welcome/index" and wrong link , now link is : "?login/index" And session don't save . Please help me . Here's my code Login.php (Controller) if($this->input->post('email') != '' && $this->input->post('password') != ''){ if ($this->user->CheckLogin($this->input->post('email'),$this->input->post('password')) == true) { $this->load->library('session'); $this->session->set_flashdata('email', $this->input->post('email')); redirect('welcome/index', 'refresh'); } else { $data['error_message'] = '* Email or Password is incorrect'; $this->load->view('login',$data); } }else{ $this->load->view('login',$data); } Welcome.php (Controller) class Welcome extends CI_Controller { public function index() { $this->load->model('user'); $this->load->helper('url'); $this->load->library('session'); $data['email'] = $this->session->flashdata('email'); $this->load->view('welcome_message',$data); } } welcome_message.php (View) <?php // Cant print email echo $email; ?> .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If you enable Query Strings then change the redirect like this redirect('c=welcome &m=index', 'refresh'); Additionally it may required to write index.php? or only ? before c Or if change configuation in config.php then you need change the c and m like $config['enable_query_strings'] = FALSE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; for more info http://www.codeigniter.com/user_guide/general/urls.html#enabling-query-strings
how can send variable value through pagination links?
SOLVED I was trying to create pagination on the home page. my default controller : $route['default_controller'] ="home"; problem 1: for pagination if I write- $config['base_url'] = base_url() . 'home/index'; // index function $config['uri_segment'] = 3; $config['per_page'] = 10; $limit = array($config['per_page'] => $this->uri->segment(3)); then it works but it shows 'localhost/baseurl/home/index/10' on browser address bar.I want to show just 'localhost/baseurl/10' (while 'localhost/baseurl/' is my homepage and I want pagination on there).so I wrote $config['base_url'] = base_url(); // index function $config['uri_segment'] = 1; $config['per_page'] = 10; $limitt = array($config['per_page'] => $this->uri->segment(1)); but that doesn't work. how can I do that ? problem 2: how can I send a variable value through pagination link like localhost/baseurl/home/index?search=y/10 I wrote: $config['base_url'] = base_url() . 'home/index?search=y'; but this doesn't work. it receives search=y/10. not only y and the pagination doesn't find 10 on $this->uri->segment(3)) so whats the correct way to do that? Edit: my htaccess code is:- <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./mysite/index.php [L] </IfModule> this code shows my site address like 'localhost/mysite/'. but if I set the base url of the pagination like $config['base_url'] = base_url(); $config['uri_segment'] = 1; $config['per_page'] = 10; $limitt = array($config['per_page'] => $this->uri->segment(1)); then the number shows on pagination link like 'localhost/mysite/10' but the value '10' not received by the script. if I write- $config['base_url'] = base_url().'home/index'; // 'index' is the function in 'home' class $config['uri_segment'] = 3; $config['per_page'] = 10; $limitt = array($config['per_page'] => $this->uri->segment(3)); then it works but the browser address line becomes 'localhost/mysite/home/index/10' what I don't like. whats the way to solve the first problem ?
For the first problem you will have to define a rewrite rule as in the first comment. For the second problem do like this, if($this->uri->segment(3)){ $keyword = $this->uri->segment(3); } else if($this->input->get('search')){ $keyword = $this->input->get('search'); } $config['base_url'] = base_url().'home/index/'.$keyword.'/page/'; $config["uri_segment"] = 5;
Is it possible to map mod_rewrite URL's in reverse?
I have a simple URL rewriting setup with a .htaccess file. Excerpt: RewriteEngine On RewriteRule ^home(/)?$ index.php?mod=frontpage&com=showFrontpage RewriteRule ^signup(/)?$ index.php?mod=usermanager&com=showRegistrationForm This works perfectly fine. However, so does a request on the old style URL. When such a request comes in, I want to perform a 301 Permanent Redirect to the SEO friendly URL, however I cannot seem to figure out how I would map /index.php?mod=frontpage&com=showFrontpage to /home. Would I have to parse the .htaccess file and do some Regex hacking for this? The URL rewriting was introduced pretty late into the project so the PHP script isn't 'aware' of the URL rewriting that's taking place; the .htaccess file is the only place this data is saved...
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^/index.php$ RewriteCond %{QUERY_STRING} ^id=(.*)$ RewriteRule ^(.*)$ /home/%1? [R=302] example.com/index.php?id=uri will redirect to: example.com/home/uri
You can accomplish this in two ways: via .htaccess via phpcode In either ways, be sure to not fall into a rediretion loop. Via .htaccess Use RewriteCond to check the QUERY_STRING and redirect depending on the querystring. Don't forget to add the [L] flag to prevent Apache to continue executing other rewrite rules and the R=301 flag to redirect the client. Via php Here you must distinguish between request from the client and requests from the server. You might want to change your rewrite rule and pass an additional parameter, example RewriteRule ^home(/)?$ index.php?mod=frontpage&com=showFrontpage&server=1 Then, in your code, check whether the parameter exists and redirect if not.
In case anyone's interested, I solved this myself using this (probably absolutely horrible) piece of PHP code. class clsURL { static function checkURL() { // don't allow requests on old style URL's if (($_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']) == $_SERVER['REQUEST_URI'] && $_SERVER['REQUEST_METHOD'] != 'POST') { // redirect to new style URL $redirectTable = self::createReverseTable(); $currentURL = $_SERVER['REQUEST_URI']; if (substr($currentURL, 0, 1) == "/") $currentURL = substr($currentURL, 1); $newURL = self::getReverseURL($currentURL, $redirectTable); if ($newURL) { header ('HTTP/1.1 301 Moved Permanently'); header ('Location: /' . $newURL); exit; } } } static function getReverseURL($current, $reverseTable) { // filter out some common stuff $current = preg_replace("/&mid=[0-9]+/", "", $current); foreach ($reverseTable as $pair) { if (preg_match("|" . $pair['from'] . "|", $current)) { return preg_replace("|" . $pair['from'] . "|", $pair['to'], $current); } } // nothing found return false; } static function createReverseTable() { $file = fopen('.htaccess', 'r'); $reverse = array(); while ($line = fgets($file)) { if (stripos($line, 'RewriteRule') === 0) { $parts = preg_split("/[\\s\\t]/", $line, 3, PREG_SPLIT_NO_EMPTY); $regex = trim($parts[1]); $url = trim($parts[2]); $parts[2] = $url; $matches = array(); if (preg_match_all("/\\$[0-9]/", $url, $matches)) { $matches = $matches[0]; // why? don't know. $from = str_replace('?', '\\?', $url); $to = $regex; foreach ($matches as $match) { $index = substr($match, 1); $bracket = 0; for ($i = 0; $i < strlen($regex); ++$i) { if (substr($regex, $i, 1) == "(") { $bracket++; if ($bracket == $index) { $pattern = ""; $j = $i + 1; while (substr($regex, $j, 1) != ")") { $pattern .= substr($regex, $j, 1); $j++; } $from = str_replace('$' . $index, '(' . $pattern . ')', $from); $to = preg_replace('/\\(' . preg_quote($pattern, '/') . '\\)/', '\\$' . $index, $to, 1); } } } } // remove optional stuff that we don't use $to = str_replace('(-(.*))?', '', $to); // remove ^ and (/)?$ $to = substr($to, 1); if (substr($to, -5) == '(/)?$') $to = substr($to, 0, strlen($to) - 5); $from = '^' . $from . '$'; // index.php is optional $from = str_replace('index.php', '(?:index\\.php)?', $from); $reverse[] = array( 'from' => $from, 'to' => $to ); } else { $from = str_replace('?', '\\?', $url); $to = $regex; // remove ^ and (/)?$ $to = substr($to, 1); if (substr($to, -5) == '(/)?$') $to = substr($to, 0, strlen($to) - 5); $from = '^' . $from . '$'; // index.php is optional $from = str_replace('index.php', '(?:index\\.php)?', $from); $reverse[] = array( 'from' => $from, 'to' => $to ); } } } fclose($file); return $reverse; } }