Welcome Guest, Not a member yet? Register   Sign In
Remove index.php from URL
#1

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!
Reply
#2

(This post was last modified: 04-30-2015, 12:10 AM by wolfgang1983.)

(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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

(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.
Reply
#4

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

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.
Reply
#6

(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
Reply
#7

(This post was last modified: 04-30-2015, 01:03 PM by ivantcholakov. Edit Reason: Address )

@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.
Reply
#8

@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?
Reply
#9

(This post was last modified: 04-30-2015, 02:18 PM by ivantcholakov. Edit Reason: Another link )

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...th-anchors

Edit:
http://webdesign.tutsplus.com/articles/q...-cms-21399 - a better explanation.

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

(This post was last modified: 04-30-2015, 02:17 PM by CroNiX.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB