Default Controller not loading after rerouting - codeigniter

I have a codeigniter multistie install where I have such code that I can serve sites with such links
site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars/
subdom.site.com/controller/function/vars
the challange is that , whith the routing
$route['sites/([^/]+)/(.*)'] = '$2';
$route['default_controller'] = "content";
I get working the links like
site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars
By idea when I go to
www.site.com/sites/sitename/
the default controller is not loading.
I made the config.php so that regarding the link structure, when I visit link
site.com/sites/sitename/controller/function/vars
then
$config['base_url']="http://site.com/sites/sitename";
if I go to
site.com/controller/function/vars
then
$config['base_url']="http://site.com/";
for the second case the default controller loads perfectly. For the subsite case not/
I get just 404
What to do?
UPDATE 2:
I have a multifolder setup.
When user goes to www.site.com/sites/site_name
then a folder of application
/root_folder/usersites/site_name is loaded.
When user goes just site.com/controller/function/var1/var2
a default application folder which is
/root_folder/application is loaded
when user goes to sub1.site.com application folder
/root_folder/domains/sub1_site_com is loaded
So when I enter in address bar
http://site.com/sites/site_name
it should be like no URI. and should load default controller.
// Application folder loading code
$myApp = '';
if($_SERVER['SERVER_ADDR']=='127.0.0.1'){
$main_url='site.com';
}
else{
$main_url='site1.com';
}
//echo $main_url;
switch($_SERVER['HTTP_HOST'])
{
case $main_url;
$uri_string=$_SERVER['REQUEST_URI'];
$link_way=explode('/',$uri_string);
if(strlen($uri_string)>6 and $link_way[1]=='sites' ){
//print_r($link_way);
//var_dump($link_way);
//checking if the link goes to usersites and sitename is bigger and =5
if($link_way[1]=='sites' and strlen($link_way[2])>=5){
$myApp='sites/usersites/'.$link_way[2];
define('SITE_ALIAS','1|'.$link_way[2]);
}
elseif($link_way[1]=='sites' and strlen($link_way[2])<5){
exit('Username should be more than 4 chars');
}
}
else{
define('SITE_ALIAS','0|'.str_replace('.','_',$_SERVER['HTTP_HOST']));
$myApp = 'application';
}
break;
default:
$myApp = str_replace('.','_',$_SERVER['HTTP_HOST']);
$myApp=str_replace('www_','',$myApp);
define('SITE_ALIAS','2|'.$myApp);
$myApp='sites/domains/'.$myApp;
}
$application_folder = $myApp;

What you appear to be doing is looking for a controller with the 'sitename' you are passing through. So if you navigate to site.com/sites/my-site/ you route tells it to look for a controller called my-site and run the index method.
The value of the route should be a path to an actual controller / method pair.
$route['sites/([^/]+)/(.*)'] = '$2';
should be
$route['sites/([^/]+)/(.*)'] = 'sites/index/$1/$2';
This is assuming it's the index method that accepts the sitename as it's first parameter in your sites controller.

It's not totally clear what you're asking, but heres a shot:
You need to create an .htaccess file in the root of your site (i.e. in the same folder that your system folder is in). In that file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
See the "Remove the index.php file" section of this page: http://codeigniter.com/user_guide/general/urls.html for more info.
Also, the route that you're using would make it so that when you go to www.site.com, you will see www.site.com/sites/content.
With the url www.site.com/sites/sitename/content/, sites is your controller, sitename the method or function, and content would be considered a parameter to the sitename function -- this won't work like it seems like you want, but I can't be sure without seeing your controller.
Maybe edit your question and add your controller(s), and we can be of more assistance.
UPDATE:
1: $config['base_url'] has nothing to do with routing or which controller is being used, so this is making your question harder to understand.
2: It isn't clear what you are trying to accomplish (sorry).
By idea when I go to
www.site.com/sites/sitename/
the default controller is not loading.
According to the CI user guide:
CodeIgniter can be told to load a
default controller when a URI is not
present, as will be the case when only
your site root URL is requested. To
specify a default controller, open
your application/config/routes.php
file and set this variable:
So, what this means is that the default_controller is used only when there is no URI present. In other words: the default controller only applies when the URL is www.site.com, and in no other case will it be used (unless you are using sub-folders in the controllers folder -- see below).
If you trying to make it so that each of your sites has its' own controller, you could use subfolders in your controller folder.
In routes.php:
$route['sites/(:any)'] = "$1";
$route['default_controller'] = "content";
Then your folder structure:
So you have your controller folder. In it, create a folder for each site. In each of those controllers create your default controller (named content.php in the above image).
With this setup, www.site.com/sites/site1 will call the default controller (content) from application/controllers/site1/content.php and show the index function of that controller.
If you then want to call other functions of the site1 controller, the URL would look like:
www.site.com/sites/site1/content/otherFunction.
Hope this helps.

$uri_string=$_SERVER['REQUEST_URI'];
$way=explode('/',$uri_string);
/// print_r($way);
//echo $way[3];
if($way[1]=="sites" and strlen($way[2])>2 and strlen($way[3])<1){
echo "JAJA ";
$route['sites/:any'] = "content/show_page/home";
}
else{
$route['sites/([^/]+)/(.*)'] = '$2';
}
this was solution. thanks all who answered. thanks stormdrain. you pointed me to a write direction in your routing example. Thanks

Related

Why Codeigniter3.0's url can't be blank after doamin name?

I'm corrently develop webs application for selling products online by using Codeigniter and upgrade from Ci2.2.1 to CI3.0.
However I've meet an errors 404 Page Not Found when I enter domain name on url not yet called any controller by hoped default_controller will load instead of type my main controller.
Notes: It is working on Ci2.2.1 and it stop work when upgrade to CI3.0.
It is working if I type any characters or any Controller after domain name(http://localhost/Ecom3/sometroller) But it is coudn't call default_controller and keep empty or blank after domain (http://localhost/Ecom3/).
Errors log: ERROR - 2015-04-24 03:48:57 --> 404 Page Not Found: /index
Please check my webs structure below:
Main->Main_controller->MY_controller->CI_Controller
My purpose to make it easy to controller the templates.
In MY_controller I've use __autoload() function
function __autoload($class){
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'libraries/' . $class .'.php'))
{
include_once $file;
}
}
}
And I have confige route as below
$route['default_controller'] = "main/Main";
$route['(:any)'] = 'main/main/index/$1';
$route['c'] = 'cat/cat/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The problem:
My url can't be blank after I type domain or after forword slash as below image
Please help
I try your code and get 404 when Main.php in your controller place inside controller/main folder. but when im move Main.php into controller folder, default controller to Main has working. then im try to debuging and see:
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
{
$method = 'index';
}
in core/Router.php
thats mean codeigniter3 are not allowed you to set default_controller in sub folder. you must put default_controller in app/controller/ to make it works.
in config/router.php
change this:
$route['default_controller'] = "main/Main";
TO:
$route['default_controller'] = "Main";
then move your controller file :
app/controller/main/Main.php
TO
app/controller/Main.php

how to make url as pretty url in codeigniter

I am new to ci. naybody knows how to minify the url.
for example : yourdmain.com/blog/view/blog-title
I need this url to be like this : yourdmain.com/blog-title
please explain how to do this
this can be many like blog, categories, pages, posts
please help..
Use route.php under your config folder
$route['blog-title] = 'blog/view/blog-title';
if you need dynamically loading based on a title
$route['(:any)/index.html'] = 'blog/view/$1';
// will route any url with /index.html to your controller
$route['(:any).html'] = 'blog/view/$1';
// will route any url with title.html to your controller then pass your title as your function variable
Why index.html or .html
This is my way i use to distinguish my other urls to my blog titles urls .. meaning only urls with extension index.html or .html will be routed to my blog/view path
You can have hyphens instead of underbars putting these lines in the file routes.php
$route['(.+)-(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4_$5";
$route['(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4";
$route['(.+)-(.+)-(.+)'] = "$1_$2_$3";
$route['(.+)-(.+)'] = "$1_$2";

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

Codeigniter admin controller in subfolder issue while pagination

I am new to codeigniter.i have create subfolder for admin controller like
Controller->admin->news.php
Now when i am access the news controller its working fine like
http://mysite.com/sacha/adminenter code here/news
But when i am trying edit delete or pagination like
http://mysite.com/sacha/admin/news/index/1
or
.../sacha/admin/news/1
Its showing 404 page not found error
Routes which i am using is
$route['admin/news'] = 'admin/news';
$route['admin/news/index'] = 'admin/news/index';
$route['admin/news/(:num)'] = 'admin/news/$1';
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';
I used (:any) also but none is working.
Thanks
remove your four line you specified above and just update your routes as this,.
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';
$config['uri_segment'] = 4;
You have to include this config parameter in pagination.

How to route everything to default controller in codeigniter?

I have a website http://example.com built using codeigniter 2. the default controller is
$route['default_controller'] = "domain";
If I try to access pageX, the link should be http://example.com/en/domain/view/pageX.
I want to allow the visitor of the website to access this page by typing
http://example.com/pageX
I have tried
$route['(:any)'] = "view/$1"; ==> it gives 404 Page Not Found
$route['(:any)'] = "domain/view/$1"; ==> it redirects to homepage with link shown as http://example.com/en/pageX
$route['(:any)'] = "en/domain/view/$1"; ==> it gives 404 Page Not Found
but non of them worked for me.
EDIT
by adding this:
$route['(:any)'] = 'domain/view/$1';
$route['en/blog'] = 'domain/view/blog';
example.com/blog will work fine
but I need it to be more general to cover all pages except admin page, something like this:
$route['(:any)'] = 'domain/view/$1';
$route['^(?!admin).*'] = 'domain/view/$o';
//The above routes will show the home page only for whatever i try!!
What is the route that i have to add to routes.php?
$route['default_controller'] is invoked if there is no URI present. Use $route['404_override'] for a full "catch all."
To get your routing pattern working, try this:
$route['[^/]*/(.*)'] = 'en/domain/view/$1';
I have managed my problem by doing this
$route['^[a-z]+$'] = 'domain/view/$1';
$route['([a-z]{2})/([a-z_]{1,50})'] = 'domain/view/$2';

Resources