0

How can I never show index.php? For example, all requests to
site.com/index.php/controller are redirected in the browser address bar to
site.com/controller?

My current .htaccess removes index.php but when a user directly types site.com/index.php/controller they are still shown that address in the address bar as opposed to site.com/controller

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

NOTE: Before flaming, I have checked lots of .htaccess threads that solve redirecting index.php but I haven't found one to never show index.php. Here are a few...
Not Show index.php in subdirectory
remove index.php in codeigniter

Community
  • 1
  • 1
csi
  • 9,018
  • 8
  • 61
  • 81

3 Answers3

0

Try:

RewriteEngine On
RewriteBase /

RewriteRule ^index.php(.*)% ^/$1 [QSA,R]

I have not tested this, but as I understand it, it will take an URL with index.php as the filename (regardless of anything following it) and redirect it to /(.*) (i.e. anything that followed index.php in the original request). The QSA flag appends the query string, and R makes it a hard Redirect rather than just rewriting the URL on the current request.

imkingdavid
  • 1,411
  • 13
  • 26
0

For anyone looking for a more generalized solution:

Options +FollowSymLinks -MultiViews

#Disallow listing contents of subfolders
Options All -Indexes

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If index or index.php requested, strip and redirect
RewriteCond %{THE_REQUEST} index(\\.php)?
RewriteRule ^index(\\.php)?$ http://yourdomain.com/ [R=301,L]

## Part 1
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## Part 2 - handle incoming that lacks extension (except for existing folders)
## To redirect /dir/foo to /dir/foo.php, (skip for admin or user dirs)
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Try adding the following to your .htaccess file in the root of your domain

RewriteEngine On
RewriteBase /

#if you get a request for this /index.php/controller
RewriteCond %{REQUEST_URI} ^/index.php/controller$
#redirect to  just controller
RewriteRule . controller     [R=301,L]

If you need this to work for any path_info use the rule below instead

RewriteEngine On
RewriteBase /

#if you get a request for this /index.php(any-path)
RewriteCond %{REQUEST_URI} ^/index.php/(.+)$
#redirect to  any-path
RewriteRule . %1     [R=301,L]
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
  • I think ```site.com/index.php/controller``` was just an example... he surely needs placeholders. – Alessandro Desantis Nov 28 '11 at 18:29
  • I have tried this but with no luck. Additionally I am working with the Codeigniter framework but I don't believe that would make a difference. Thanks but any other ideas? And yes on the placeholders. – csi Nov 28 '11 at 21:17
  • What exactly isn't working? You're not getting redirected or you're getting a 500 Internal Server Error? – Alessandro Desantis Nov 29 '11 at 20:25
  • I am not being redirected. If I access site.comp/index.php/controller that remains in the address bar as opposed to being taken to site.com/controller. Thanks for the help and any ideas are appreciated to solve this. – csi Nov 30 '11 at 02:37