Codeigniter routing using base_url - codeigniter

I am using the Pagination library in Codeigniter, and the links it creates are exactly what I'm looking for. When I click on the link to go to the next page I get a 404.
My base url (index page) is: http://localhost/foo
My routing is:
$route['default_controller'] = 'main';
$route['page/(:num)'] = 'main/page/$1';
I want to be able to go to the url: http://localhost/foo/page/2 to get the second page. main is the controller. page is a function inside main.
I can type in the url http://localhost/foo/index.php/main/page/2 and get to the correct page, but I do not want to have the index.php/main in the url.
What am I doing wrong?

You aren't doing anything wrong. If you want to remove the index.php from the url:
Leave in blank $config['index_page'] attibute on the application/config/config.php file and make sure that you have a .htaccess that contains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Also make sure the apache server has mod_rewrite enabled and in the configuration of apache AllowOverride attribute it's been set to All

In you Controller(In pagination code) $config['base_url'] should be
$config['base_url'] = base_url() . 'foo/page/';
In routes.php
$route['page/(:num)'] = 'main/page/$1';
$route['page'] = 'main/page';//add this line
In .htaccess,(Place outside application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Note: To use base_url() in config/autoload.php
$autoload['helper'] = array('url');//url
and in config/config.php, (Change this)
$config['base_url'] = '';
$config['index_page'] = '';

Related

Default URL is not working in Codeigniter

I am new in Codeigniter framework. I am using HMVC design pattern
I have a issue with base url when I enter URL like this https://sitename/ its show blankpage and
when enter URL like this https://sitename/controller then show it main page of website
Config.php
$config['base_url'] = 'https://sitename/';
Routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'notify/show_404';
$route['translate_uri_dashes'] = FALSE;
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
ErrorDocument 404 index.php
Options -Indexes
How to show main page of website on this URL https://sitename/

CodeignIter with HTTPS page not rendering domain/H.html not working(404 Error)

Website is normally working without HTTPS.
with HTTPS only homepage is working but other pages like (https://domain.in/contact.html) not working(404 Error Page)
My Files.
application/config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
.httaccess file is
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Please suggest me what to do.
Thanks in Advance.

removing index.php in hmvc

i want to removing index.php in hmvc code igniter , when i type the url without index.php it's running well but when i process to other controller the url is automatically showing index.php after my codeigniter folder.
example i type :
localhost/ci_hmvc/user/login
and the result when i process to other controller is :
localhost/ci_hmvc/index.php/user/dashboard
and my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci_hmvc/index.php/$1 [L]
</IfModule>
please any one help me
You can make changes in config.php which is in config folder
You can see the line
$config['index_page'] = 'index.php';
Change this line in to
$config['index_page'] = '';
I hope this will helps you
put this code in .htaccess and place the htaccess file to the root of the folder..
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]

Remove index.php from Codeigniter URL in Wamp

I have big problem trying to remove index.php from url's
i have searched lots if forums (this also), but no help for my situation.
so i have config.php file
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
my .htaccess file in public_html folder near index.php.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
i have rewrite_module ON (and it's working) and my httpd.conf AllowOverride set to All
so when i am going to mydomain.com/controller/action - I get 404
but when i go to mydomain.com/index.php/controller/action - everything is OK..
i have tried lots of .htaccess examples... and so on.. nothing helps..
.htaccess (This should be place outside application folder)
Paste this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Note : Sometime index.php will not remove in windows. But if you upload it to server it will work fine. This happen me too in XAMPP but if i upload it to host(Linux) it works fine
My Config which is working fine :
config.php
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
.htaccess
# index file can be index.php, home.php, default.php etc.
DirectoryIndex index.php
# Rewrite engine
RewriteEngine On
# condition with escaping special chars
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

How should I minimize url in CodeIgniter 2.2.3

I am new in codeigniter and I love to use it in my future projects too.
I having a big time problem to make my url user friendly.
I have a website running that has a lot of pages, so URLs get too big and they look too bad.
So what I want to do, is shorten my URLs. For example:
www.abc.com/index.php/main/home
I want to make something like this:
www.abc.com/home
So my question is, How should I do this thing in CodeIgniter 2.2.3 ? Is there any demo?
I was see so many links for it but it can not full fill my requirement so please help me..
You can use routes for this purpose
To remove index.php use .htaccess with following code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
and in config.php
$config['uri_protocol'] = 'AUTO';
now in your routes application/config/routes.php
$route['home']='main/home';
To remove index.php, place .htaccess outside application folder (root)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
To short URL, use config/routes.php
$route['home'] = "main/home";
Ex:
To use this I'm Moving
Firstly i create .htaccess file and put this code in it..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /xyz.co/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
than go to application/config/routes.php and put below code.
$default_controller = "auth";
$controller_exceptions = array('main');
$route['default_controller'] = $default_controller;
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
$route['404_override'] = 'error';
$route['translate_uri_dashes'] = FALSE;
and in config.php
$config['uri_protocol'] = 'AUTO';
thats all...

Resources