How to create the controller for http://localhost in Codeigniter? - php

I am using codeigniter and was hondering how to create the home page controlller, the controller for http://localhost. As of now i can access all controllers that are http://localhost/{controller}, but not just localhost. My .htaccess is as follows:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|sandbox|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/test/$1 [L]

Set the default_controller to the name of the controller you want to use in routes.php

Related

how to set homepage in Codeigniter?

I organized my controllers and views into subdirectories like this
frontend
admin
I created a Home.php in controllers/frontend
and a
home.php file in views/frontend
CI is installed in http://localhost/ci
when I access http://localhost/ci, I'd like the home view to load, but currently I can only access it via http://localhost:8080/ci/frontend/home
I'm guessing it may have something to do with my .htaccess file which is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also created the following constant:
define('BASE_URL', '/');
In the routes.php the default controller is set to:
$route['default_controller'] = 'frontend/home';
In the config.php, the base URL is set to:
$config['base_url'] = BASE_URL;
What am I doing wrong?
Thanks
Out of the Box CI 3.x.x doesn't support what you want to do. It's strictly controller/method...
But a little digging found this - Extending the Router Class to support subfolders
This allows you to override the behaviour of the router class and use sub folders.

Codeigniter routing on shared host

I am trying to move my codeigniter application from my WAMP installation to a shared host (justhost) for the first time.
In my public_html folder I have included all of the folders and files for my application. So for example I see my "application" folder when I open public_html in my file manager.
I have a controller "Lander" which I have set to be the default via $route['default_controller'] = 'Lander'
When I go to www.mywebsite.com I do see the view that gets loaded from the Lander controller. The problem is that within that view I have a link Log in that, when I'm running on WAMP, opens the login controller, which is in the same folder as the Lander controller. When I click this link from the hosted site the URL goes to www.mywebsite.com/Login, but I get 404 not found.
In Config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # remove index.php
.htaccess should be
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
and Links should be
Log in
There should be a controller call login or in routes you should define

Redirect home URL to main domain

I want to redirect home controller to my domain, it should look like this
http://sample.com/home to http://sample.com
http://sample.com/home/index to http://sample.com
http://localhost:8888/sample/home to http://localhost:8888/sample/
http://localhost:8888/sample/home/index to http://localhost:8888/sample/
is it possible? And how to do it?
When you need to redirect to home page redirect('/');
URL Helper
On your config base_url() make sure is set
$config['base_url'] = 'http://sample.com/';
You may need to remove index.php and have a suitable htaccess file in main directory.
$config['index_page'] = '';
Htaccess example for main directory
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Your home controller route should the be set on default route application -> config -> routes.php
Codeigniter URI Routing User Guide Page
$route['default_controller'] = 'home';
Then autoload url helper
$autoload['helper'] = array('url');
The if you need to redirect to home controller make sure file name Home.php
and use redirect('/');
Note: Codeigniter 3 is case sensitive make sure first letter all ways upper case on file name and class name

Codeigniter: Redirect predefined urls to default controller's method

There are two predefined URLs which I want to redirect to default controller's method and return default responses. URLs are:
URL 1: http://EnrollmentService.mydomain.com/EnrollmentServer/Discover.svc
(GET request)
URL 2: https://EnrollmentService.mydomain.com/EnrollmentServer/Discover.svc (POST request)
I tried adding following to .htaccess file but no luck.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /testsaav/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Redirect /enrollmentserver/Discover.svc http://localhost/projectname/
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Right now I am trying to redirect http://localhost/projectname/EnrollmentServer/Discover.svc to default controller's index method.
I have created a project with default controller name enrollmentserver.php but when I tried to access http://localhost/projectname/enrollmentserver, I got Object not found error
How would redirect both the urls to default controller's any method?
You should use the default CI routes system. In your application/config/routes file:
$route['EnrollmentServer/Discover.svc'] = "projectname";
Or generically:
$route['EnrollmentServer/:any'] = "projectname";
Assuming "projectname" is the controller you want to route to

CodeIgniter URL Routing

My current url is http://localhost/ci/ which is also set under config.php
but how do I open other pages in views without specifing index.php
http:/localhost/ci/Pages/view/login but not like this http:/localhost/ci/index.php/Pages/view/login
You should take a look at this
http://www.freewebmasterhelp.com/tutorials/htaccess/
you can do alot with .htaccess file
try putting this in your .htaccess file
RewriteEngine on
RewriteCond $1 ^(Pages|view|login|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Use application/config/routes.php file.
Add URL Routing definition is very simple with it.
See this link for more information: http://codeigniter.com/user_guide/general/routing.html

Categories