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!

Subscribe
Notify of
guest

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

21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
21
0
Would love your thoughts, please comment.x
()
x