How to Change the Name of index.php in WordPress

They say it can’t be done. Read support requests in the WordPress forum and you’ll see lots of people saying it’s not possible to change the name of index.php to blog.php or site.php or anything else.php. But it can be done. I know it. I’ve done it. Here’s how…

Change the Name of index.php

Changing the name of index.php is very straightforward. In these examples we will rename index.php to blog.php

  1. Open .htaccess file of the root site for editing
  2. Change every instance of index.php to blog.php
  3. Change every instance of index.php to blog\.php to blog\.php
  4. Add the directive DirectoryIndex blog.php index.php to the very top of .htaccess
  5. Save and close .htaccess.
  6. Rename index.php to blog.php

Be aware that index.php might return or be overwritten when you upgrade or reinstall WordPress. Only worry about this if you have a non WordPress file called index.php in the root directory at the same time as you have a differently named index file for WordPress.

Write-protect any non WordPress index.php file in the root directory to prevent it being overwritten. Create a backup of the file just in case you need to restore it.

If you rename index.php to site.php or hidden.php or to anything but blog.php then change blog.php in the above instructions to whatever name you use.

Example 1: Regular WordPress Site

Change this

## This line might not yet be present.
## Should add it for security
DirectoryIndex index.php

# BEGIN WordPress

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

# END WordPress

To

DirectoryIndex blog.php index.php

# BEGIN WordPress

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

# END WordPress

Example 2: WordPress MultiSite Using Subdomains

Change this

## This line might not yet be present.
## Should add it for security
DirectoryIndex index.php

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

# END WordPress

To

DirectoryIndex blog.php index.php

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^blog\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . blog.php [L]

# END WordPress

What Does DirectoryIndex Do?

DirectoryIndex tells the server which file or script to load when a client visits a directory. The correct file to load is usually set in a server’s configuration file(s) but can be overridden.

When DirectoryIndex is used, the server will load the first stated file followed by the second stated file if the first one cannot be found followed by any successive files if their precedents cannot be found.

For example

DirectoryIndex index.php

Will load index.php when someone visits a directory.

DirectoryIndex index.php blog.php

Will load blog.php when someone visits a directory and index.php is not present in that directory.

DirectoryIndex blog.php index.php

Will load index.php when someone visits a directory and blog.php is not present in that directory.

Many .htaccess directives cascade to lower directories. In our case, we want blog.php to load when someone tries to visit our root domain. When someone tries to visit a subdirectory of our site, we want index.php to load. So, we say to the server, “Look for blog.php, load it if it’s present; if blog.php isn’t present, load index.php”.

Many WordPress directories such as wp-content/ and wp-admin/ contain an index.php file. So that we don’t need to manually rename all those index.php files, we let the server load them only if blog.php is not present in those directories.

Why Rename index.php?

You might have a hundred different reasons but two good ones are:

  1. You want to install two content management systems side by side such as Magento and WordPress. Both systems use files named index.php. Renaming the WordPress index.php to blog.php makes it possible (with a few more .htaccess directives) to install both Magento and WordPress into a site’s root domain and activate them according to the page someone is looking to view.
  2. Maybe you already have a site installed in your root directory but you want to play with WordPress or another CMS without disturbing your current site.

I’m interested to here your own reasons for giving index.php a new name.

Does Renaming index.php cause Problems?

Not that I’ve found. If you know of any, let me know.

Not a problems with site loading as such but issues that could crop up are:

  1. As suggested a few times in the comments below, when WordPress is updated or re-installed, it recreates its original index.php file. Write-protect the file to prevent this happening. Backup the custom index.php file so you can restore it if it does happen.
  2. Some security scanners might detect a ‘change’ in the default WordPress index.php file. This occurs because these scanners look for a file called index.php that is built for WordPress. Don’t worry about the scanner flagging the file unless you know the non WordPress index.php file has been altered by some process.

Sharing is caring!

21
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of

What will happen if I update WordPress? Have you checked that?

setter

This is just what I’m doing now but the problem I have is getting permalinks to work because the forum (also in root) is using index.php so wp permalinks returns a 404. Spent 11 days on this so far. Any help would be hugely appreciated.

setter

Hi Lee, many thaks for that but the problem I’m finding is because we’re already using a rewriterule in htaccess for the forum (index.php). I cant seem to make it do both for index.php and blog.php. It only works for which ever one is declared first. Did you manage to get both working to produce clean top level url’s?

Rick

Found you page via the ‘googles’. Good info, but….you have several mis-spellings of ‘directory-index’ (you left out the send ‘r’…directoy-index ).

Jonathan P

I have just been reading your article about changing the index.php to blog.php on a wordpress site. Great article. I have a site where I need a non-wordpress index.php so I have followed your instructions. Only problem is, I need it so that if someone visits the site, index.php (non wp) shows up (which I have done with DirectoryIndex stipulating index.php then blog.php. Only problem is that I want to add a link from index.php to the home page of the wordpress (blog.php) but if I use the link /blog.php it says “page not found”. Any advice or ideas? Thanks… Read more »

Jonathan P

I believe I found the answer. In wordpress, you create a new page (call it articles for example) and then go to settings -> reading and choose a static homepage and choose articles as your post page. Hope this helps someone.

JP

This works great! But one of my customer WP installations keeps overwriting .htaccess and changing the settings. Thoughts on preventing that while allowing other changes to be made to the file?

Dan

It doesn’t work.
I am getting a 404 on the domain.com/blog.php – All other sites are working fine, just the mainpage has a 404

Hello you have a great weblog over here! Thanks for sharing this interesting stuff for us! If you keep up this good work I’ll visit your weblog again. Thanks!

Sorry forgot to mention, I search replace all instances of old url, changed permalinks to blog/%tittle% and set domain name in settings prior to migration. I confined all in phpmyadmin

john

hello nice article.. i got it to work but now i cant open mystie,com/wp-admin
should i add something to htaccess? thanks in advance

Yes i have same problem and my tags link not working :(

Manu

Step 2 and step 3 of your instructions are contradicting each other, probably because there is a syntax error in step 3?

Yaten

Hi! I renamed the index to an other name. On mobile the menu disappeared. (Desktop view is OK.)
Do you have any idea ?
Thanks,
Yaten