homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

PHP5.3, htaccess, and removing index.php

 5:54 am on Dec 16, 2012 (gmt 0)

Hi guys,

I've moved my site to a new server and suddenly the htaccess file isn't removing the index.php from the url correctly. And what I mean by that is when you access a url without index.php in it, it acts as if the index.php was never loaded. For example, / (the top of the domain) and /forums and /{any url} all load the homepage of the site.

Adding in index.php, for example site.com/index.php/forums, does indeed work correctly.

With all the googling of the various ways to remove index.php using htaccess, none of them worked. My host (host gator) also requires that the following line by added to the htaccess file in order to use PHP5.3:

AddType application/x-httpd-php53 .php

I'm not sure if that's relevant or not. My current code (although I've tried many) looks like so:

AddType application/x-httpd-php53 .php

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

Thanks in advance :)



 6:04 am on Dec 16, 2012 (gmt 0)

I've had similar problem before. Try adding

RewriteBase /


RewriteEngine On

 7:28 am on Dec 16, 2012 (gmt 0)

Ah yup, I already tried and just retried it. No difference :(


 8:08 am on Dec 16, 2012 (gmt 0)

You are in luck, because it is only a few days since we sorted out a long thread from someone else who

:: cough-cough ::

apparently started out with your same host. Or his brother.

Read that thread, and then come back and tell us which parts of his problem are the same and which parts are different. The bit that jumped out at me was the business about "index.php" in the middle of the url, where God never intended there to be an index dot anything. But some hosts apparently think differently.

I think this is the one I had in mind (I originally homed in on an even longer one, so stop complaining):


It's a meandering thread, but it gets there. Or just skip all the intervening posts and read the ones from g1smd ;)

:: pause to grumble because while searching the Forums I found yet another "/index.php/more-stuff-here" question from a newbie who just disappeared-- probably to some other forum where someone spoon-fed him the answer ::


 9:43 am on Dec 16, 2012 (gmt 0)

I think he had quite a different issue. Mine is quite simple, remove index.php, so I'm looking for a one rule for every url, every page loads index.php. Unless I missed something?

It's weird because other websites that use FURL's using .htaccess, even with the same code, on the same sever, work perfectly fine. So maybe its the php, but I can't imagine what is causing this to happen...


 11:32 am on Dec 16, 2012 (gmt 0)

Well, if you've still got this in your htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

that would account for every page loading index.php wouldn't it? :)

Or do you mean that somewhere it's got an actual redirect, so the user's address bar says /index.php/ no matter what they typed?

when you access a url without index.php in it, it acts as if the index.php was never loaded. For example, / (the top of the domain) and /forums and /{any url} all load the homepage of the site.

Uhm, isn't index.php itself the home page?

If I sound confused it's because you seem to be saying two opposite things: index.php is not loading at all, AND it's loading everywhere. Maybe a few examples would help. Make sure you look at both things: what your address bar ends up saying, and what physical page you're looking at.


 11:17 pm on Dec 16, 2012 (gmt 0)

Oh yes sorry about that, I can see where you're confused. Index.php isn't exactly the homepage. It's the url that defines the homepage, using $_SERVER["PATH_INFO"]. So "/" will load the homepage data, and /forums will load the forums data. Both load their own controllers and models, etc.

Does that make sense? I also believe I've made a mistake here and it's the php that is causing the error because I have tested the above htaccess code on another website on the same sever. This is a unique error and I've completely lost, but before getting help I need to dig in and find out what I can..

Thanks for your replies :) At least I learnt a bit more about htaccess files.


 12:09 am on Dec 17, 2012 (gmt 0)

I found the solution in PHP. The PHP framework I use (called Hydrogen) relies on $_SERVER["PATH_INFO"] to load various controllers. Using FURL's, on every page $_SERVER["PATH_INFO"] always equaled nothing. It needed to be set to the same thing it equaled when a url was loaded that included /index.php/, which on my host is simply:


This one line solved the issue for me. Phew.


 8:51 am on Dec 17, 2012 (gmt 0)

I still don't fully understand the question.

If you had URLs that looked like
www.example.com/index.php/this-page and you now have URLs that look like www.example.com/this-page you do need to add a redirect so that when the old URL is requested, the user is redirected to the new one.

RewriteRule ^index\.php/(.+) http://www.example.com/$1 [R=301,L]

 12:21 am on Dec 18, 2012 (gmt 0)

The question was the simple case of removing index.php from the URL, but when i made the topic I wasn't aware that it wasn't a htaccess issue.

The site was showing the homepage because $_SERVER["PATH_INFO"] was always empty without index.php. The framework I'm using relies on $_SERVER["PATH_INFO"] to know what php classes to load. Eg /{controller}/{function}/{variable}

I'm not sure I need to redirect those URLs because none of my pages have ever linked to a URL with index.php in it. Furthermore they're returning a 404 error and most of my pages have rel canonical set.


 1:36 am on Dec 18, 2012 (gmt 0)

removing index.php from the URL

URLs are defined in links on the page, so what you are literally asking is "I want to alter the links on my page from
<a href="/index.php/pagename"> to <a href="/pagename">" yet you also say "none of my pages have ever linked to a URL with index.php in it".

So I am not sure "where" you want to remove it from, if it's not from the links on your pages.


 12:51 pm on Dec 18, 2012 (gmt 0)

Oh, I was never talking about links on the page. I always said "the URL" which refers to the address bar. Does htaccess also remove index.php from href's? I'm not sure, so I thought by being an htaccess topic that it would be assumed I'm talking about the url in the address bar. I don't know why having index.php in the href in an a tag would ever matter, aside from using since you can just edit the HTML.

I think most people who don't know much about htaccess files always assume that we're messing with the url in the address bar :P That's what we're mostly googling snippets for. Sorry for the mix up.


 2:07 pm on Dec 18, 2012 (gmt 0)

The address bar shows the URL that is going to be requested.
The address bar is usually populated once you click a link (or type something in).

htaccess cannot change the links on the page, it acts only on requests once they arrive at the server.

So, if something requests an "incorrect" URL, rules in htaccess can detect the request and respond by sending 301 status and the details of the new URL.

When that new URL is requested by the browser, the URL in the address bar is updated.


 12:43 am on Dec 19, 2012 (gmt 0)

I don't know why having index.php in the href in an a tag would ever matter, aside from using since you can just edit the HTML.

This is starting to sound a bit circular :) If something is in the href, then that is what the user's browser will request when they click on the link. And what the search engine will put on its shopping list.

Editing the html-- or the php that generates the html-- to get rid of unwanted links is an essential first step. But it doesn't retroactively change existing bookmarks, browser histories or search-engine lists. That's where you need the redirect.

If google has requested the wrong url even once, you will need a redirect forever. Or be content with google getting 404s forever. You can get rid of them faster with a 410, but it doesn't work on all search engines.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved