Post 22: CodeIgniter (Removing index.php file)

When using CI (PHP Framework), you will notice that CI includes index.php in the URL for any link to any file.  When you search for this issue by typing: “ci remove index.php from url”, you will see the link to Ellislab user guide  that describes how to fix or solve it as shown in the image below:

image for CI Help
image for CI Help

Now the explanation is very simple and clear, but unfortunately is not complete! In this post, I will try to show you what I’ve learned and did to get my project links to work without including index.php in the URL.

Step1:
When you first install CI, you will notice the .htaccess file is inside a subfolder called “Application”. You need to move it (.htaccess) to the root folder of your project. For example, if your CI website is inside a subfolder named “myproject”: www.mywebsite.com/myproject, .httaccess has to be inside myprojet folder (directory).

Step2:
Open .htaccess, clear it then write or copy the following code:

RewriteEngine On

RewriteBase /myproject/

RewriteCond %{REQUEST_URI} ^system.*

RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*

RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

ErrorDocument 404 /index.php

Notice that in the second line of the code, the CI website name is myproject. You need to change it based on your project root folder name.

Step3:
Open config.php file here is the path for this file:

Myproject/application/config/config.php

First:

You will see the following comment plus $config[‘base_url’]

// the config[base url] file is the root folder name and anything before that:

Change the value of $config[‘base_url’] to:

For localhost (on your pc):
$config[‘base_url’] = ‘http://localhost/myproject/’;

For your online server:
$config[‘base_url’] = ‘http://www.mydomain.com/myproject/’;

Second:

In the same file (config.php), you will see the line: $config[‘index_page’] = ‘index.php’;

Remove the index.php as shown below:

$config[‘index_page’] = ”;

Step4:
I assume you have your main navigation links inside php template file named header, navigation, or any name you chose. You need to change the links in your main nav to look like this:

<li><a href=”<?php echo base_url();?>mainsite/home”>home</a></li>

mainsite is your main controller name
home is the function name inside your mainsite controller that load home page from the view folder

And that’s it, I hope my post was helpful and easy to understand

Post 21: Remote SQL Connection

When we start creating a database driven website using PHP, the first thing we do is testing our application using the local host package like: WAMP for windows, MAMP for mac, or XXAMP for both. In the early stages of development workflow we might need to create a local database with simple tables using PHPMyAdmin locally, but when the application is growing larger and when the time is came to start developing and testing the project using our servers, we have to connect to a remote MySQL database. Due to many hosting providers, there are different ways to configure that type of connection, or finding the right IP address for our own servers.

Through my search earlier in the last semester, I found the table that contains a list for most of the hosting companies from codex.wordpress.org and it could be useful for many PHP developers, below is just a sample for GoDaddy and you can visit the actual page in codex.wordpress.org that contains the entire hosting.

GoDaddy – cPanel Hosting:  localhost
GoDaddy – Plesk Hosting: Use the IP address shown in the Databases Section in Plesk. Do not include: 3306

If your hosting company using cPanel which is built on Linux platform, in this case, we need to enable remote MySQL from cpanel in order to connect our PHP files that are running on our PCs using local Apache server with the MySQL database on the server. By enabling remote database, I mean adding the IP address for your computer to the web server host list in cpanel.

In windows, there are different ways to know your IP address by following either one of the following steps:

First Way:  using CMD

  • Go to run command and type: CMD to open command prompt window

runWindow

  • Type: ipconfig and this will list the info of your network connection.
    The value that we are concerning about is IPv4 Address: XXX.XXX.X.XX.

IPaddress

Second Way:

  • Go to Control Panel then type Adapter in the search box.
    From the search result, double click the View Network connections to open your network if it’s Ethernet or Wi-Fi, network
  • Double click the one that in use (the activated one)
    From the Wi-Fi / Ethernet Status window, click on details and you will see the value of IPv4 Address for your current PC.

By the way, if nee to know IP address for your URL, you can also open the CMD window and type: ping www.YourDomainName.com. Or when you login into your cpanel you will see in the left pane Shared IP Address which is the same.

Finally, you need to log into your cpanel (www.YourDomainName.com/cpanel) to add your PC IP address as a remote web server to communicate with the database in your hosting company. And to do that, you can double click the remote MySQL server, then type your IP address in the box and click Add Host button as shown below:

cpanel

MySQL