There are many tutorials that talk about deleting index.php from the url. But I want one step forward of it.
The link is: www.example.com/index.php/controller/function/single-variable
I want it like: www.example.com/some-name-that-i-put/single-variable
I cannot find a tutorial to do such a thing. How to do it?
i wasnt sure whether you were wanting to do direct URL rewrites how CI does it out of the box or not... but this is how you do URL Rewrites:
your class/function/id example.com/news/article/13 transposes to: example.com/class/function/ID
http://codeigniter.com/user_guide/general/urls.html
you can set this by doing two things:
in your config.php file under application/config/ edit these:
approx line 80: $config['uri_protocol'] = 'REQUEST_URI'; // i set this to request_URI as it always works for me..
approx line 93: $config['url_suffix'] = '.html'; // this is optional, if you want /13.html
approx line 62: $config['index_page'] = ''; //set this to blank
create a .htaccess in http root:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
This is how you do URI Route changes
if you want to change the organization of the URI you can use URI Routes defined in routes.php as described here: http://codeigniter.com/user_guide/general/routing.html
Check out this CodeIgniter user guide.
Try this codeigniter user guide
https://www.codeigniter.com/user_guide/general/routing.html
Try this
in your config file place these
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
$config['index_page'] = '';
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = FALSE;
after this make a .htaccess file at your root directory and put this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
this will Definitely help you
Related
My CodeIgniter project is located in
C:/xampp/htdocs/Project/CodeIgniter/myproject
And I am using Xampp.
I set a base URL like this but it is not working.
$config['base_url'] = 'http://localhost:3000/myproject';
How I can run this project locally.
Quick debug time.
So what happens if you just use http://localhost in the url?
You should see whatever is in C:/xampp/htdocs/. So if you have a index.php in there, that should show up. If you don't have an index.php, then create one and see what happens.
Unless you have done "other things" in your setup, your Server Document Root will be C:/xampp/htdocs/. So it will be expecting your projects to be under that folder.
Now, you have decided to put your project under C:/xampp/htdocs/Project/CodeIgniter/myproject
So you would need to use http://localhost/Project/CodeIgniter/myproject
Do you get the Codeigniter welcome ( NOTE: if you haven't altered the default route)
A very quick fix, using your current settings, would be to relocate your project to
C:/xampp/htdocs/myproject as this is where the server is expecting to see it with the URL you are using.
What happens?
Personally, I never use localhost and create a virtual host which from memory XAMPP allows you to do from its management window. That way your project files can be located anywhere as long as the server knows where to find them. I will leave that as a project for you to look at.
How does your .htaccess file looks like?
It should look like this:
RewriteEngine on
RewriteBase /myproject
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
Replace
$config['base_url'] = 'http://localhost:3000/myproject';
To
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] = preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
And your .htaccess file should look like this :
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|css|js|install|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
check your project path
and site base url
$config['base_url'] = 'http://localhost/your_project_path/';
if you have port
$config['base_url'] = 'http://localhost:3000/myproject';
in application/config/config.php
after that index.php is run
and set database connection if you want to conncet db
in your_project/application/config/database.php
db['default']['username'] ='your_db_root_user';
$db['default']['password'] = 'your_db_root_password';
$db['default']['database'] = 'database_name';
// The following values can probably stay the same.
$db['default']['hostname'] = 'localhost:3000';
$db['default']['dbdriver'] = "mysqli";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = true;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
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'] = '';
I am new to the world of web development. I have a website hosted. When I login it should take me to "member_dashboard.php". It redirects to this page on localhost. If I modify the localhost path, the remote redirect gets affected. I am using codeigniter as the framework. "base_url" is left blank. I tried setting the base url with the full http path of my index page still cannot fix it.
$config['base_url'] = '';
Do this changes as well
In config/routes.php
$route['default_controller'] = "";//default controller name
$route['404_override'] = '';
In config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
In config/autoload.php
$autoload['helper'] = array('url');
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>
The value on this key must be different on each environment. It file can not be included on control version. Considerate set it on .gitignore
$config['base_url'] = '';
//localhost environment
$config['base_url'] = 'http://localhost/';
//other env
$config['base_url'] = 'http://mypublicsite.com/';
Suppose your website name is example.com
So just keep
$config['base_url'] = 'http://example.com/';
under your application>config>config.php
Believe me your problem will solve because I personal did it and its working.
Have any questions or clarification, please let me know.
Thanks,
Amzad
I created a project using CI(version 3.0.0), and it works perfectly in XAMPP localhost. And i upload that project to sub domain then its giving an error Click here.
I Handle Router's and config setting as well. So what is this error?? how to avoid this?? There are some answers, But not suit for this.
Config.php
$config['base_url'] = '';
$config['index_page'] = '';
Routes.php
$route['default_controller'] = 'shop';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Change the name of your controller, model, and other your class to Capitalize.
Example :
your own controller test.php change to Test.php
It should be work.
Its because your server is Linux, and Linux is case sensitive.
CodeIgniter beginner here. The base URL of my website is 'http://localhost/routing/'.
// config.php
$config['base_url'] = 'http://localhost/routing/';
I'm simply trying to route the url 'http://localhost/routing/admin' to the admin controller using the following rules but it doesn't work. Instead I have to use 'http://localhost/routing/index.php/admin'.
$route['default_controller'] = 'seasons';
$route['admin'] = 'admin';
$route['404_override'] = '';
Question: is there a way to remove 'index.php' from the url?
Make changes in .htaccess file and config.php
application/config.php
$config['index_page'] = '';
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
is there a way to remove 'index.php' from the url?
Yes, as well as being a very popular question on SO, it is covered in the CodeIgniter documentation (which is very good and I highly recommend reading).
In your .htaccess write this. Also look for additional information : Codeigniter Guide
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt) # exceptions
RewriteRule ^(.*)$ /index.php/$1 [L]