34

I'm trying to redirect index.php files to the root /, I've searched around and found several snippets of code similar to:

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ http://www.domain.com/$1 [R=301,L] 

This however won't work for subdomains, example below:

  1. I have a subdomain: subdomain.domain.com
  2. A user visits http://subdomain.domain.com/index.php
  3. Using the htaccess above they are redirected to http://www.domain.com/ rather than http://subdomain.domain.com/

Does anyone know how to adjust the htaccess above so it will take into account the subdomain and redirect them to the appropriate location?

e.g. if a user visits http://subdomain.domain.com/index.php they will go to http://subdomain.domain.com/

Bonus Points:

Is there a htaccess that can just apply this rule to all folders?

So for any folder with an index.php they will just be redirected to it's root e.g. http://subdomain.domain.com/folder1/folder2/index.php would automatically go to http://subdomain.domain.com/folder1/folder2/

Alex
  • 1,018
  • 1
  • 11
  • 21

3 Answers3

92

Do this:

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
  • 2
    This worked great, thanks! For anyone with the same problem, if you have a subdomain, you might be forced to select a folder for it to sit in, make sure you create/select a folder out of public_html or this htaccess will try and redirect to that folder. e.g. i have subdomain.domain.com which had it's files stored in www.domain.com/subdomain, this htaccess would send users to subdomain.domain.com/subdomain whenever they visited subdomain.domain.com/index.php but if you place the folder outside of public_html you don't get this problem. – Alex Feb 21 '12 at 22:31
  • @ThinkingMonkey Could you fix the dot in the regex `index.php`? I believe you intended to write `index\.php` :) – caiosm1005 Sep 15 '13 at 22:56
  • 1
    @ThinkingMonkey I realise this is 5 years old, however I was wondering if it was possible to redirect "/index" as well as "/index.php"? I have code that removes any .php extensions to make the url's cleaner, however the I'd like domain.com/index to redirect to domain.com if possible? – BN83 Apr 12 '17 at 12:17
  • @ThinkingMonkey Don't you need a `\.` in the second `index.php`, too? – Chuck Le Butt May 15 '19 at 11:04
  • @ThinkingMonkey And isn't the `RewriteCond` redundant in this case? – Chuck Le Butt May 15 '19 at 11:32
  • `RewriteRule ^index\.php/(.+)$ /$1 [R,L]` This worked for me – Homayoon Ahmadi Nov 08 '21 at 14:46
  • @ChuckLeButt The `RewriteCond` directive that checks against `THE_REQUEST` is required if you are internally rewriting the request to `index.php` later (a front-controller pattern). Otherwise, it probably is "redundant". – MrWhite May 16 '22 at 22:36
  • @HomayoonAhmadi Although that's solving a different problem. And in the context of this question, would not remove `index.php` so would not be a solution to this problem. – MrWhite May 16 '22 at 22:38
18

I based the following code on @ThinkingMonkey's answer

RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]

It redirects URIs ending with index.php, index.htm or index.html to /. Works with subdirectories, too.

Also, notice the NC flag. It makes it case-insensitive. So it works even if the URI is in upper-case such as INDEX.PHP.

Community
  • 1
  • 1
caiosm1005
  • 1,686
  • 1
  • 19
  • 31
-4

Can be use this

RewriteRule ^index\.php/(.*)$ http://www.example.com/$1 [R=301,L]
Rich Bowen
  • 5,872
  • 2
  • 17
  • 15
Rahul Dadhich
  • 1,213
  • 19
  • 32
  • That certainly appears to be the exact opposite of what was requested. – Rich Bowen Sep 28 '16 at 19:53
  • This is the best solution. Other works only if you have domain.ext/index.php but does not works for doamin.ext/index.php/some_argument/ -> domain.ext/some_argument/ . – T. Popović Jan 04 '17 at 00:24
  • @T.Popović But this is answering a _different_ question than that posed above. So, in that sense, it doesn't actually answer the question! – MrWhite May 16 '22 at 18:18