CodeIgniter Forums

Full Version: Removing index.php from url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Computerzworld[/eluser]
Hello. I am having problem in removing index.php from url. I made one htaccess file for this and also removed index.php from config file like this.
Code:
$config['index_page'] = "";
Here is my htaccess code.
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|uploads|jscripts|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

This works perfectly in my windows environment but doesn't works with linux environment. In windows all the urls are without index.php and the same code I have uploaded to linux server I have to write index.php after url for the first time after it works. I don't know what is the problem behind it. Please help me. Thanks in advance.

El Forum

[eluser]adamfairholm[/eluser]
I'm not sure this is the answer you're looking for, but it is what I do to overcome this problem.

The .htaccess effectiveness seems to vary from server environment to server environment. Sometimes there even needs to be a Wiki article for a certain hosting [email=http://codeigniter.com/wiki/Dreamhost_.htaccess/]provider[/email].

So there may be other forum members who can correct me, but it my experience its unrealistic to have one .htaccess file in your dev environment that will work on different production server environments.

What I've started doing is creating two config variables:

Code:
$config['base_url'] = "http://localhost/ot/";
$config['link_url'] = "http://localhost/ot/index.php/";

So on my dev environment I don't have to worry about .htacces, and when I get the site to the production server, I find an .htaccess that works, and change my configs to:

Code:
$config['base_url'] = "http://localhost/ot/";
$config['link_url'] = "http://localhost/ot/";

Kind of redundant and inelegant as a solution, but it works for me. If you search for .htaccess on the forums there are a ton of different .htaccess files to try for different environments. In my case, I wasn't really knowledgeable about .htaccess so I tried different ones until I started taking more educated stabs at what they server will like.

El Forum

[eluser]Computerzworld[/eluser]
thanks for reply... so... which modifications I have to make in order to change config variable from base_url to link_url?

El Forum

[eluser]adamfairholm[/eluser]
You don't have to change base_url to link_url, but the idea is you have them both side by side, and they are different - one has the index.php and one doesn't.

This is handy if you are linking to an image - you don't want the index.php, so you'd use:

Code:
<img >config->item('base_url');?&gt;images/my_pic.png" />

However, if you had a link, on your dev environment you need that index.php because you have no .htaccess file doing its thing. Then you'd use:

Code:
<a >config->item('link_url');?&gt;">A link to some awesome stuff</a>

When you move your project to its server where it will make its home, then you take the index.php off of link url and:

Code:
<img >config->item('base_url');?&gt;images/my_pic.png" />
<a >config->item('link_url');?&gt;">A link to some awesome stuff</a>

Are going to have the config variable replaced with the same thing.

You can easily add another config variable in Code Igniter. Right below the base url config item, just add:

Code:
$config['link_url'] = "http://yourdevenviormenturl/index.php/";

Does that make sense?