Alex Alex - 7 months ago 53
Apache Configuration Question

htaccess redirect index.php to root (including subdomains)

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$$1 [R=301,L]

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

  1. I have a subdomain:

  2. A user visits

  3. Using the htaccess above they are redirected to rather than

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 they will go to

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. would automatically go to


Do this:

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]