mvc configuration on web server - php

I recently uploaded my codeigniter project onto my web server.
it's in a folder called project1 this is what my directory view looks like
[www.myurl.com]
|-> project1
|-> application
|-> css
|-> images
|-> js
I am having two problems.
The first is that I want it to load my welcome view when I go to (www.myurl.com) and not
(www.myurl.com/project1/main_controller/index.php/welcome). How do i get rid of all that garbage?
The second problem is when I use <?php echo base_url();?> it shows up fine in view source but when I click on it, it echo's the url [www.myurl.com/project1/www.myurl.com/project1/] why is it doing this?

It looks like you've installed CodeIgniter into a subdirectory. If you want your CI site to load from the root url, you should copy it one directory up so the Application & System folders are directly under the root url of your web directory. From there, the .htaccess rules that are posted by shail (and quoted below) should take care of removing the index.php in your paths.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ci/index.php/$1 [L]
Regarding the site_url() issue, make sure you have your $config['base_url'] variable set in application/config/config.php and that it contains the trailing slash.
$config['base_url'] = "http://www.myurl.com/";
To test this further, try passing a parameter to the site_url() function like this:
echo site_url(welcome);
Which should return: http://www.myurl.com/welcome if everything is set right.

Getting CI up and running is rather simple. Most of it requires you to edit a few configuration
files.
You need to setup CI to point to the right base URL of the app. To do this, open up system/application/config/config.php and
edit the base_url array item to point to your server and CI folder.
view plaincopy to clipboardprint?
$config['base_url'] = "http://localhost/ci/";
Currently, the CI setup will have a default controller called “welcome.php”; you can find this in the system/application/controllers folder. For this tutorial, delete it and open your system/application/config/routes.php file. Change the default array item to point to the “helloworld” controller.
view plaincopy to clipboardprint?
$route['default_controller'] = "Helloworld"
CI also has a view file that we do not need. Open up the system/application/view/ folder
and delete the welcome_message.php file.
You can change your default controller here.
In order to edit Url http://localhost/ci/index.php/helloworld/.There are a few things you can do to improve your CodeIgniter experience -
like removing that annoying index.php bit out of the URL. You can accomplish this task by creating a .htaccess file in the root folder, and adding the following code.
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ ci/index.php/$1 [L]
You’ll also need to open up the config.php file in system/application/config/ and edit the index_page array item to a blank string.
view plaincopy to clipboardprint?
$config['index_page'] = "";
Another nifty trick is to turn on CI’s ability to parse PHP alternative syntax if its
not enabled by the server. To do this, open up the same file as before, system/application/config/config.php, and set the rewrite_short_tags array item to TRUE.
view plaincopy to clipboardprint?
$config['rewrite_short_tags'] = TRUE;
I hope all goes well!

Related

I want to run my codeigniter project on localhost, without defining 'base_url' in config

I downloaded a codeigniter folder from its official website. Setup it in wamp.
I remove index.php by making setting in .htaccess file.
'I left blank base url in config.'
Now, when i click on link to go to another controller, instead of showing http://localhost/projectname/cntrlname url is http://127.0.0.1/projectname/cntrlname.
I dont want to set base url. so, what should i do to make it work properly.
if you want to run your project without define base_url in config.php file.
You must keep some points in your mind.
1- Define base url as blank in config.php file.
$config['base_url'] = '';
2- You also leave index_page as blank in config.php file.
$config['index_page'] = '';
3- You must load helper url in autoload.php file, which is define in config folder.
$autoload['helper'] = array('url');
4- Suppose your project folder name test. then i write htaccess for that project for example.
RewriteEngine on
RewriteBase /test/
RewriteCond $1 !^(index\.php|cache|images|js|css|fonts|robots\.txt)
RewriteRule ^(.*)$ /test/index.php/$1 [L]

mod_rewrite and routing to code igniter

I have been using code igniter as a subdirectory of a site and have several instances of if on one server. Each instance is considered a separate application, but all under the same domain name. I am looking to enable a slightly different URL structure though and have turned to mod_rewrite to help me out.
The page is reachable at http://localhost/test but I want to rewrite that URL to appear as http://localhost/en-US/test.
The problem is I keep getting a code igniter (CI) 404. I can confirm the mod_rewrite is reaching the CI index.php, but CI is failing to deliver the correct page. Here's and example setup:
1) Download a new instance of CI and place it in a subdirectory in the site root. Name the CI folder "test". It should be reachable now on a local server at http://localhost/test and you should see the default welcome view.
2) Create a .htaccess file in the server's root web directory. DO NOT place it in the CI "test" directory. Add the following lines to it.
RewriteEngine On
RewriteBase /
RewriteRule ^en-US/test/?$ test/index.php [NC,L]
From my understanding of mod_rewrite, this should allow the URL http://localhost/en-US/test to render what is located at http://localhost/test. It doesn't though. I just get the CI version of a 404. I need help figuring out how to resolve this.
Hmm your question made me install CodeIgnoter and read its routing :)
Anyway have your DocumentRoot/.htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^en-US/(test)/(.*)$ $1/$2 [NC,L]
Then replace this line in your /test/application/config/config.php`:
$config['uri_protocol'] = 'PATH_INFO';
It is by default set to:
$config['uri_protocol'] = 'AUTO';
After this you can open this URL: http://domain.comen-US/test/ to load CI home page in /test/ dir.
You should check out routing. Place this statement in your config/routes.php file AFTER the reserved routs.
$route['test'] = "en-US/test";
You can forget about htaccess when you have Codeigniter (most of the times).

Codeigniter: Unable to access the Stylesheets

I have the following directory structure of Codeigniter Folder. You can see that I have put all my assets in the assets folder at the root of the application.
Base URL is defined as
$config['base_url'] = 'http://kamran.dev/Codeigniter/application/';
I tried modify it to
$config['base_url'] = 'http://kamran.dev/Codeigniter/';
but that didn't work either.
Can anyone please have a look and tell me what I'm doing wrong here?
P.S. Then I removed the .htaccess file from the application folder that contains
Deny From All
And this worked for me. But that doesn't seem like a good idea.
You have multiple options here. The easiest one is to move the assets folder to the same level as the application so that your directory structure is:
application
system
assets
css
img
js
Because the assets folder is not in a folder with the .htaccess with Deny From All it won't be blocked by your web server.
Alternatively you can add an .htaccess file in the assets folder with the following rule:
# /assets/.htaccess
Allow from all
I haven't tested this but I think it should work.
Another option is to change the .htaccess and put an exception on the assets folder but it's a very bad idea (with security implications) to remove the Deny From All without writing alternate rules to lock all folders that shouldn't be accessible.
Also, set your base_url to the folder containing application, system and assets directories (this folder contains Codeigniter's bootstrap/front controller index.php).
Additionally, I would advise you to echo your urls using the base_url() function:
base_url("assets/css/bootstrap.min.css");
OR to call a controller/route
base_url("controller/method");
The base_url function takes into account the value of $config['index_page'] as well as the necessary leading/trailing slashes.
Change your base_url to the following:
echo base_url("assets/css/file-name-here.css");
You want to pass the URL string to the base_url function. This way, you can change $config['base_url'] to whatever you want and it will append the string to that URL properly.
As an addition to the answer of grim. If you have your rewrite engine on you should have the following in your root .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

Codeigniter 2.0 default controller for sub folder not working in server but working in localhost

I have set up a virtual host in my local machine (assume it to be http://local.he). I have a home.php in the main ‘controllers’ folder. I have two subfolders inside the ‘controllers’ folder and they are ‘admin’ and ‘wori’ and each has a home.php file.
Per CI 2.0 structure, if I access http://local.he/module/wori then it should load home.php from ‘wori’ and it is working but when I did the same in the server after uploading the files it always loads from module. Even if I access something like this: http://site.com/module/wori/users, it is still loading the home.php from module.
Here is the .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|static)
RewriteRule ^(.*)$ index.php/$1 [L]
Here is the routing and these two are the only executable lines in routes.php
$route['default_controller'] = "home";
$route['404_override'] = '';
When I try to access http://site.com/module/index.php/wori or http://site.com/module/index.php/wori/users then it works. I checked .htaccess and it is working for other modules in my site. I even tried this example: CodeIgniter default controller in a sub directory not working but this is still not working:
$route['wori'] = 'wori/home';
Can anyone tell me what is missing?
I followed the answer from the following url and it worked for me.
http://codeigniter.com/forums/viewthread/181149/#857629
yet would like to know why it was working normally like in the localhost.

CodeIgniter Controllers functions is not working

I have deployed my site on a VPN server. I am able to access the controller constructor, but when I am trying to call any method like mydomain/controller/method, it is not working. However 'mydomain/controller/index' is also not working.
When I am debugging with php method function_exists('my function name') then also it is coming up false even though that method exist in my controller.
Please help with this problem, and let me know if I have to do any config changes.
Thanks!
You will need to navigate to http://mydomain/index.php/controller/method
... unless you specifically have a .htaccess file in the root where index.php is that eliminates index.php from the URL.
If you have this .htaccess file set correctly you will be able to navigate to http://mydomain/controller/method
here is an example of an .htaccess file for codeigniter that I'm using for debugging.
php_flag display_errors on
php_value error_reporting 7
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
copy all of this text to a file called .htaccess and put it where index.php is in the codeigniter root directory.
///////////////// Edit based on more info from OP //////////////////
First test to see if the following works..
Unless you deleted it, CI provides you, by default a welcome controller (/application/controllers/welcome.php) and a welcome view thats being called by the controller (/application/views/welcome_message.php).
See if these files are there and if they aren't get them from the zip file in the codeigniter framework and put it in these directories.
navigate to the config folder and open routes.php (/application/config/routes.php) and under reserved routes put in $route['default_controller'] = "welcome"; if it's not already there.
In addition make sure you have the Config changed to reflect the fact that you are using the .htaccess file.
the property should be set as follows in config.php $config['index_page'] = '';
Note that if you are not using .htaccess this would be set to $config['index_page'] = 'index.php';
These instructions are in an effort to get you to see something on the page. You would change the default controller to your controller (not welcome) when you get going with the above.

Categories