CodeIgniter Forums
Remove index.php from URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Remove index.php from URL (/showthread.php?tid=61606)

Pages: 1 2


Remove index.php from URL - eci35 - 04-30-2015

Following the instructions at http://www.codeigniter.com/userguide3/general/urls.html, I can get the index.php removed from the URL of regular links, e.g., http://www.foo.com/index.php, using this .htaccess entry:
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Now, there is a scenario where there is a redirect inside of a controller that should go to something like http://www.foo.com/bar/foo However, the redirect goes to http://www.foo.com/index.php/bar/foo instead. Directly typing http://www.foo.com/bar/foo works fine. It's just the redirect that adds the index.php for some reason.

The redirect is very simple:
PHP Code:
   public function index() 
 
   
    
redirect('/foo/bar''data');
 
   

How do I can get rid of the index.php in the URL of a redirect? Thanks in advance!


RE: Remove index.php from URL - wolfgang1983 - 04-30-2015

(04-30-2015, 12:01 AM)eci35 Wrote: Following the instructions at http://www.codeigniter.com/userguide3/general/urls.html, I can get the index.php removed from the URL of regular links, e.g., http://www.foo.com/index.php, using this .htaccess entry:



Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Now, there is a scenario where there is a redirect inside of a controller that should go to something like http://www.foo.com/bar/foo However, the redirect goes to http://www.foo.com/index.php/bar/foo instead. Directly typing http://www.foo.com/bar/foo works fine. It's just the redirect that adds the index.php for some reason.

The redirect is very simple:



PHP Code:
   public function index() 
 
   
    
redirect('/foo/bar''data');
 
   

How do I can get rid of the index.php in the URL of a redirect? Thanks in advance!

Have you removed the $config['index_page'] = 'index.php'; to $config['index_page'] = '';

Also I have a whole list of different htaccess you could try here Codeigniter Htaccess Github


RE: Remove index.php from URL - eci35 - 04-30-2015

(04-30-2015, 12:10 AM)riwakawd Wrote: Have you removed the $config['index_page'] = 'index.php'; to $config['index_page'] = '';

Also I have a whole list of different htaccess you could try here Codeigniter Htaccess Github

Setting $config['index_page'] in the config.php file worked. Thank you!

The list of different htaccess will definitely come in handy. Thank you for the reference.


RE: Remove index.php from URL - gard_olsen - 04-30-2015

I managed to remove index.php with .htaccess, now I cannot get to subfolders like css, js and so on. Any suggestions ? Smile


RE: Remove index.php from URL - CroNiX - 04-30-2015

How are you loading your css/js? Show the code. Also, where are css/js subfolders located? They should be in the site root, not in /application or /system dirs.


RE: Remove index.php from URL - eci35 - 04-30-2015

(04-30-2015, 08:01 AM)CroNiX Wrote: How are you loading your css/js? Show the code. Also, where are css/js subfolders located? They should be in the site root, not in /application or /system dirs.

Agreed. Something like this:
Code:
-www
--application
--css
---foo.css
--js
---bar.js
--system



RE: Remove index.php from URL - ivantcholakov - 04-30-2015

@eci35

Before: http://my-site.com/index.php/
Now: http://my-site.com/

The browser interprets the additional segment index.php as an additional directory level. If in your code you are using relative links to your css, js, images, etc., they would become broken after removing the segment index.php. Use absolute URLs.

Code:
<img src="<?php echo base_url('assets/img/my-image.png'); ?>" />

If you insist on relative links, there is another trick by using the tag <base href="<?php echo base_url(); ?>" /> within the document head, but I don't use it, it brings side-effects on anchor links.


RE: Remove index.php from URL - eci35 - 04-30-2015

@ivantcholakov

Thank you for the clarification. So far, I've been using absolute URL's to be safe. And I saw echo base_url(); but did not know that it brings side-effects. What sort of side effects are you referring to? Performance-related?


RE: Remove index.php from URL - ivantcholakov - 04-30-2015

Code:
<a href="#section-1">Section 1</a>

Also, often you can see links like

Code:
<a id="action" href="#">Action</a>

The base tag will modify behavior of anchor links like these and additional manual corrections would be needed.

http://www.ninthavenue.com.au/blog/using-base-href-with-anchors

Edit:
http://webdesign.tutsplus.com/articles/quick-tip-set-relative-urls-with-the-base-tag--cms-21399 - a better explanation.

Again, for a web application I would not recommend this way. Use absolute URLs for assets.


RE: Remove index.php from URL - CroNiX - 04-30-2015

Personally I just use
Code:
<img src="//img/my-image.png" />
<script src="//js/some-script.js" type="text/javascript" charset="utf-8"></script>

Etc. and skip the php