.htaccess not acting as suspected - php

I'm using CI and WAMP on a Windows 7 machine. In my Application folder of the site I have a .htaccess file with the following:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
This was copied and modified CodeIgniter User Guide. And I made sure the rewrite_module (didn't it used to be mod_rewrite?) was active and restarted WAMP. In my controller, I have the following:
<?php
class Forum extends CI_Controller
{
public function index()
{
$data['title'] = "Card Forums";
$this->load->view('Header', $data);
$this->load->model('forumdb');
$data['sections'] = $this->forumdb->getSections();
$this->load->view('Home', $data);
$this->load->view('Footer');
}
public function site()
{
$data['title'] = "Card Forums - Site";
$this->load->view('Header', $data);
$data['section'] = "Site";
$this->load->view('Forum', $data);
$this->load->view('Footer');
}
}
When I go to localhost/forum it does exactly as I want. But when I try to go to localhost/forum/site it is displaying the following:
Not Found
The requested URL /forum/site was not found on this server.
In the view Forum I have:
<?php echo $section; ?>
Which is only supposed to display the the variable right now because I want to make sure I'm accessing the page first (just started writing this site Friday). When I try localhost/forum/index.php/site it's giving me a 404 error.
I have a feeling I'm missing something VERY simple here and it's bugging me. Any help would be appreciated.

Here is an updated .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ index.php/$1 [L]
In your /forum/config folder find the config.php and change this line
$config['index_page'] = 'index.php';
To
$config['index_page'] = '';
Then as you mentioned you can use function like base_url('css/site.css');
One thing I generally do with this type of htaccess file is add an entry for a folder called public so in my case it would look like this.
RewriteEngine on
RewriteCond $1 !^(index\.php|public)
RewriteRule ^(.*)$ index.php/$1 [L]
Then I have all my assests in the public folder, and I don't need to keep adding exceptions to my htaccess file.

For WAMP (and most other environemnts for my CI projects), I've had most luck with the following additional flags in .htaccess:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
Also, Check your uri_protocol in config.php. I've had most luck with setting it to PATH_INFO instead of AUTO.
One last thing to try: CI sometimes detects the base_url wrong in WAMP environments. Either define your base_url in config.php.

Related

How do I use codeigniter in a subfolder on a server?

I have two codeigniter installations under my public_html folder on my server. One is directly under public_html and the other is under public_html/posts_receive.
The instance under public_html is working normal but when I try to access the codeigniter controller inside public_html/posts_receive it shows:
404 Page Not Found
tried url http://www.example.com/posts_receive/index.php/dashboard
To solve this problem I have added an .htaccess file under public_html/posts_receive which contain the following:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /posts_receive
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
My controller is named dashboard.php and contains the following:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library("pagination");
}
public function index() {
$config = array();
$data['link_sel']='dashboard';
$data['stats']=$this->fetch_stats();
$this->load->view("header_inc", $data);
$this->load->view("dashboard", $data);
$this->load->view("footer_inc", $data);
}
}
?>
config.php includes:
$config['base_url'] = 'http://www.example.com/posts_receive/';
$config['index_page'] = 'index.php';
You should run both applications from one CodeIgniter base. You will have 2 sub folders in your application folder. Take a look at the documentation: https://www.codeigniter.com/userguide3/general/managing_apps.html
From there on you can create the same index.php file as in your root in your posts_recieve folder and point it to the right application directory. That is how i managed to do it. Otherwise the CodeIgniter instance in your root will think you are navigating to a controller called posts_recieve which does not exists in that application.Hope this helps. Otherwise, just ask when things are unclear.
Inside .htaccess, RewriteBase /posts_receive part is not needed.
RewriteRule ^(.*)$ index.php?$1 [L]
should become
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
(<IfModule mod_rewrite.c> is a server configuration condition. It means you should have mod_rewrite module installed and active on the web server for these settings to work.)
You could leave empty $config['index_page'] inside config.php:
$config['index_page'] = '';
And you can call the controller without the index.php part:
http://www.example.com/posts_receive/dashboard

CodeIgniter Redirect on Heroku

I'm having some issues with CodeIgniter redirects on Heroku. Here is the URL:
http://sergei-game.herokuapp.com/
In particular, what is happening is that the redirects (/controller/function) are being appended to the URL rather than replacing /controller/function in the URL.
I do not have these issues when I navigate to the same URL on localhost.
Here is the relevant code in the main controller:
public function index() {
redirect('gameplayer/login', 'refresh');
}
public function login() {
if (isset($_SESSION['email'])) {
// if logged in, go to character selection page
redirect('gameplayer/viewAccount');
} else {
$this->load->view('auth/login');
}
}
Here is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I took a look at the Heroku logs, but they don't make very much sense to me.
You don't have the correct base URL configured in CodeIgniter. In the configuration file located at application/config/config.php find the line $config['base_url'] and set it to your URL. In your case it should be $config['base_url'] = 'http://sergei-game.herokuapp.com/';

CodeIgniter - Default controller not loading automatically

I have a default controller set:
$route['default_controller'] = "Home";
However, when I go to http://mydomain.com/, it says 404 Page Not Found, but when going to http://mydomain.com/Home, the controller loads fine. What could be the problem? I've been wracking my head for hours. My htaccess is posted here if needed. Thanks!
Turns out my problem was somewhat unrelated. I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work. When CI looks for the default controller file, it searches in lowercase for the file; if I name my controller file "Home.php" instead of "home.php," CI misses it on Linux (since Linux file systems are case sensitive).
There is nothing wrong with your routing, the problem is with your htaccess file. Try removing
ErrorDocument 404 /index.php
You have probably some .htaccess problem.
Tray this way:
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
config.php
$config['base_url'] = 'www.homepage.com';
$config['index_page'] = '';
routes.php
$route['default_controller'] = "yourcontrollername";
$route['404_override'] = '';
Without any additional information & code from your configs/controllers, I'd suggest checking config/config.php for these (both should be empty in normal cases):
$config['base_url'] = '';
$config['index_page'] = '';
IIRC, I have seen similar issue and it was related to these config variables. Hopefully this helps you a bit.
I sure this .htaccess solved all ubuntu users...
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
I have a default controller set:
$route['default_controller'] = "home";
it's working for me.

How to remove controller name from url making it clean in codeigniter

I have the following url..
http://localhost/ci/site_controller/home
I want to remove site_controller controller from url resulting in..
http://localhost/ci/home
How can I do this in CodeIgniter ?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.
EDIT
I have this in my routes.php
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!
.htaccess
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php
You can set your a route for each url:
In your config/routes.php file, just set each page like this:
$route['ci/home'] = "ci/site_controller/home";
This might help you to define a Default Controller for your CodeIgniter project.
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'site_controller';
assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/
This helped for me. In "routes.php" I added this:
$route['(:any)'] = "default_controller/$1";
try using the routes file to re-map url's
http://ellislab.com/codeigniter/user_guide/general/routing.html
I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you :)
In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...
RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And your codeigniter code lies directly in the html folder...
To remove the controller name from URL, edit application/config/routes.php
Add lines like these:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin
To pass variables into said function, do the following:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web, which has a parameter. Function p() was defined as:
public function p($param = "none") {
echo $param;
}
I hope this helps :)
It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x
in my case URL was: http://localhost:8080/index.php/page/sometext
Where "Page" is the controller name
open application/config/routs.php
$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";
Result: http://localhost:8080/index.php/sometext
I assure you, it will work. tested 100%.
Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:
$route['home'] = "site_controller/home";
Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.
You're complicating things. No need for routes at all.

Codeigniter routing issues

I am customizing my routes in codeigniter, here is a sample of my routes.php:
$route['default_controller'] = 'maincontroller/main';
$route['404_override'] = '';
$route['test'] = 'maincontroller/test';
here is the maincontroller class:
class Maincontroller extends CI_Controller
{
public function __construct( )
{
parent::__construct( );
$this->load->library('session');
$this->init( );
}
public function main( )
{
echo "in main";
}
public function test( )
{
echo "test";
}
}
but when i access this url in my browser: /test, i get the server's 404 page not found error.
I have no idea what im doing wrong, i followed the routes guide in codeigniter user guide.
Instead of going to /test , try going to /index.php/test
By default CodeIgniter runs everything through index.php.
You can change this behaviour by adding a .htaccess file to the site root folder.
This is taken straight from the CodeIgniter user guide.
https://ellislab.com/codeigniter/user-guide/general/urls.html
The htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
If you do this you also need to change your config/config.php file
find
$config['index_page'] = 'index.php';
change it to
$config['index_page'] = '';
When you want to create links in a page (view) you can use the built in site_url() function, you may need to load the url helper for this.
for example
<?php echo site_url('test'); ?>
This will take into account the $config['index_page'] setting and create the right url for your link.
if you are trying to run your site from localhost use the below code..
adding a .htaccess file to the site root folder (e.g: mysite).
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /root folder/index.php/$1 [L]
for the site hosted on the remote hosting server, use this code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories