Hello everyone, welcome to my today’s post on “How to make URL SEO friendly in CodeIgniter”. I’ve a good news for you that from today I’m going to start a new tutorial session on codeigniter. I’ll post useful and effective tips on CodeIgniter regularly that will help you to built an efficient CodeIgniter(CI) application.
So, First of all lets learn few words about CodeIgniter and it’s features.
What is CodeIgniter:
CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications.
I love CodeIgniter because It’s have some awesome features. Like- exceptional performance, small footprint, run a variety of PHP versions and configurations, requires nearly zero configuration, does not require you to use the command line and finally provide all kinds of efficient functions that we need our development time.
Well, It’s time to get back in to today’s topics, Let’s go-
– Only 2 steps for making your url SEO friendly:
Most of the CodeIgniter users had faced this issue, when they installed CodeIgniter in their local server for the first time. Issue was that you can’t see any contents in your application without setting a “index.php” text before your controller name. If you trying to access your application without “index.php” you will get a 404 page. So, what we need to do for solving this issue? It’s so simple. Just follow the steps:
Step: 01 SEO friendly urls doesn’t contain index.php!
Go to you config folder inside application folder. You will found a config.php file in config folder. Open it. & go to line no: 29 . Remove index.php text from there. Have a look attached example:
/* |-------------------------------------------------------------------------- | Index File |-------------------------------------------------------------------------- | | Typically this will be your index.php file, unless you've renamed it to | something else. If you are using mod_rewrite to remove the page set this | variable so that it is blank. | */ $config['index_page'] = 'index.php'; //Remove index.php text
Hope you’ve already remove that texts from config file. Now move for the step 02. We need to write some htaccess code for defining URL structure.
Step: 02 Write htaccess for SEO friendly url:
Go to your parent folder of your application and create a file called .htaccess. Follow the steps to create .htaccess file. Open your NotePad application and go to File > Save As > Change the “Save as Type” to “All Files” > enter “.htaccess” as the name and press Save and we are done 🙂 Your .htaccess file has been created.
If you can’t open a file then you can download it from download section below the tutorial. Now open htaccess file in a editor and write codes like below –
RewriteEngine On RewriteCond %{REQUEST_URI} ^/system.* RewriteRule ^(.*) index.php?/$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+) index.php?/$1 [L]
Save it and we are done. Now you don’t need to add index.php before controller name. Your URL format will be something like this-
http://www.yourdomain.com/controller/method/parameter1/
That’s all for today. I’ve attaced full htaccess code , so that you can download and test yourself. Best of luck 🙂
Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!