Question

I am wondering, is it possible to remove index.php from an URL? Basically on some pages in a site I have this structure,

http://www.domain.com/index.php/members/register, but other pages I have URL structures like this, http://www.domain.com/category/products/id/5, I want to know is it possible with htaccess to remove the index.php and any attributed slashes when needed? How would I go about doing this?

Was it helpful?

Solution

Yes, you can. With this rule any requested /index.php will be removed:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]

But you should better use the proper URLs right from the start so that you application is serving documents whose links don’t contain the /index.php.

OTHER TIPS

If you want to globally rewrite index.php/controller/action

This .htaccess configuration should do the trick:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule> 

This configuration checks on Apache whether the file/directory exists on disk or not (i.e. the request match a real resource on disk), and rewrite the request to your front controller if needed.

So http://www.domain.com/resources/image.png should return the image resource. And http://www.domain.com/user/show/5 should transparently rewrite to http://www.domain.com/index.php/user/show/5

With this configuration, you can remove all index.php references in your application URLs and leave the rewriting to the web server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top