- WordPress: Change the Default Index Page Using .htaccess
- How To Change the Default Index Page
- Default index php wordpress
- The Template Hierarchy In Detail
- Home Page display
- Front Page display
- Privacy Policy Page display
- Single Post
- Single Page
- Category
- Tag
- Custom Taxonomies
- Custom Post Types
- Author display
- Date
- Search Result
- 404 (Not Found)
- Attachment
- Embeds
- Non-ASCII Character Handling
- Filter Hierarchy
- Example
WordPress: Change the Default Index Page Using .htaccess
By default, most servers that host websites are configured to use a file called index (.html/.htm/.php/etc.) as the default index page for a website. However, by using some .htaccess code, you can change this so that you can name your default index page to whatever you want. The focus of this article is to show you how to do just that! If you have not already created a .htaccess file or are not certain how to edit this file, please refer to our guide: Creating and editing a .htaccess file. Let the custom index naming commence!
How To Change the Default Index Page
Essentially this rule instructs the server to search for a file other than your normal index file (e.g., index.php) and serve it as the default directory index. It is important to note that the .htaccess file with this rule in it must exist in the same directory as the index file that you wish to replace:
#Tells the server to serve an alternate index page DirectoryIndex new-creative-filename-here.html
#Tells the server to serve an alternate index page DirectoryIndex index.php
This rule is slightly different and instructs the server to scan the directory the .htaccess file is in for a list of files defined in the rule. It will then serve the first match it encounters. Please note that the list is read from left to right:
#Tells the server to serve the first match from the list as the default index page DirectoryIndex new-creative-filename-here.html index.php index.CGI new-creative-filename2.htm
This neat little trick not only allows you to be a bit more creative but can also help you organize different sections of your site. For example, let’s say your site uses PHP and has a FAQ and Downloads section. You could name their index files faq.php and downloads.php, respectively.
Written by Michael Brower / June 22, 2017
Default index php wordpress
The Template Hierarchy In Detail
While the template hierarchy is easier to understand as a diagram, the following sections describe the order in which template files are called by WordPress for a number of query types.
Home Page display
By default, WordPress sets your site’s home page to display your latest blog posts. This page is called the blog posts index. You can also set your blog posts to display on a separate static page. The template file home.php is used to render the blog posts index, whether it is being used as the front page or on separate static page. If home.php does not exist, WordPress will use index.php .
Note: If front-page.php exists, it will override the home.php template.
Front Page display
The front-page.php template file is used to render your site’s front page, whether the front page displays the blog posts index (mentioned above) or a static page. The front page template takes precedence over the blog posts index ( home.php ) template. If the front-page.php file does not exist, WordPress will either use the home.php or page.php files depending on the setup in Settings → Reading. If neither of those files exist, it will use the index.php file.
- front-page.php – Used for both “your latest posts” or “a static page” as set in the front page displays section of Settings → Reading.
- home.php – If WordPress cannot find front-page.php and “your latest posts” is set in the front page displays section, it will look for home.php . Additionally, WordPress will look for this file when the posts page is set in the front page displays section.
- page.php – When “front page” is set in the front page displays section.
- index.php – When “your latest posts” is set in the front page displays section but home.php does not exist or when front page is set but page.php does not exist.
As you can see, there are a lot of rules to what path WordPress takes. Using the chart above is the best way to determine what WordPress will display.
Privacy Policy Page display
The privacy-policy.php template file is used to render your site’s Privacy Policy page. The Privacy Policy page template takes precedence over the static page ( page.php ) template. If the privacy-policy.php file does not exist, WordPress will either use the page.php or singular.php files depending on the available templates. If neither of those files exist, it will use the index.php file.
- privacy-policy.php – Used for the Privacy Policy page set in the Change your Privacy Policy page section of Settings → Privacy.
- custom template file – The page template assigned to the page. See get_page_templates() .
- page-.php – If the page slug is privacy , WordPress will look to use page-privacy.php .
- page-.php – If the page ID is 6, WordPress will look to use page-6.php .
- page.php
- singular.php
- index.php
Single Post
The single post template file is used to render a single post. WordPress uses the following path:
- single—.php – (Since 4.4) First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12 , WordPress would look for single-product-dmc-12.php .
- single-.php – If the post type is product , WordPress would look for single-product.php .
- single.php – WordPress then falls back to single.php .
- singular.php – Then it falls back to singular.php .
- index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php .
Single Page
The template file used to render a static page ( page post-type). Note that unlike other post-types, page is special to WordPress and uses the following path:
- custom template file – The page template assigned to the page. See get_page_templates() .
- page-.php – If the page slug is recent-news , WordPress will look to use page-recent-news.php .
- page-.php – If the page ID is 6, WordPress will look to use page-6.php .
- page.php
- singular.php
- index.php
Category
Rendering category archive index pages uses the following path in WordPress:
- category-.php – If the category’s slug is news , WordPress will look for category-news.php .
- category-.php – If the category’s ID is 6 , WordPress will look for category-6.php .
- category.php
- archive.php
- index.php
Tag
To display a tag archive index page, WordPress uses the following path:
- tag-.php – If the tag’s slug is sometag , WordPress will look for tag-sometag.php .
- tag-.php – If the tag’s ID is 6 , WordPress will look for tag-6.php .
- tag.php
- archive.php
- index.php
Custom Taxonomies
Custom taxonomies use a slightly different template file path:
- taxonomy—.php – If the taxonomy is sometax , and taxonomy’s term is someterm , WordPress will look for taxonomy-sometax-someterm.php. In the case of post formats, the taxonomy is ‘post_format’ and the terms are ‘post-format-. i.e. taxonomy-post_format-post-format-link.php for the link post format.
- taxonomy-.php – If the taxonomy were sometax , WordPress would look for taxonomy-sometax.php .
- taxonomy.php
- archive.php
- index.php
Custom Post Types
Custom Post Types use the following path to render the appropriate archive index page.
- archive-.php – If the post type is product , WordPress will look for archive-product.php .
- archive.php
- index.php
(For rendering a single post type template, refer to the single post display section above.)
Author display
Based on the above examples, rendering author archive index pages is fairly explanatory:
- author-.php – If the author’s nice name is matt , WordPress will look for author-matt.php .
- author-.php – If the author’s ID were 6 , WordPress will look for author-6.php .
- author.php
- archive.php
- index.php
Date
Date-based archive index pages are rendered as you would expect:
Search Result
Search results follow the same pattern as other template types:
404 (Not Found)
Likewise, 404 template files are called in this order:
Attachment
Rendering an attachment page ( attachment post-type) uses the following path:
- .php – can be any MIME type (For example: image.php , video.php , pdf.php ). For text/plain , the following path is used (in order):
- text-plain.php
- plain.php
- text.php
Embeds
The embed template file is used to render a post which is being embedded. Since 4.5, WordPress uses the following path:
- embed—.php – First, WordPress looks for a template for the specific post. For example, if its post type is post and it has the audio format, WordPress would look for embed-post-audio.php .
- embed-.php – If the post type is product , WordPress would look for embed-product.php .
- embed.php – WordPress then falls back to embed .php .
- Finally, WordPress ultimately falls back to its own wp-includes/theme-compat/embed.php template.
Non-ASCII Character Handling
Since WordPress 4.7, any dynamic part of a template name which includes non-ASCII characters in its name actually supports both the un-encoded and the encoded form, in that order. You can choose which to use.
Here’s the page template hierarchy for a page named “Hello World 😀” with an ID of 6 :
- page-hello-world-😀.php
- page-hello-world-%f0%9f%98%80.php
- page-6.php
- page.php
- singular.php
The same behaviour applies to post slugs, term names, and author nicenames.
Filter Hierarchy
The WordPress template system lets you filter the hierarchy. This means that you can insert and change things at specific points of the hierarchy. The filter (located in the get_query_template() function) uses this filter name: «_template» where $type is the template type.
Here is a list of all available filters in the template hierarchy:
- embed_template
- 404_template
- search_template
- frontpage_template
- home_template
- privacypolicy_template
- taxonomy_template
- attachment_template
- single_template
- page_template
- singular_template
- category_template
- tag_template
- author_template
- date_template
- archive_template
- index_template
Example
For example, let’s take the default author hierarchy:
To add author-.php before author.php , we can manipulate the actual hierarchy using the ‘author_template’ template type. This allows a request for /author/username where username has the role of editor to display using author-editor.php if present in the current themes directory.
function author_role_template( $templates = '' ) < $author = get_queried_object(); $role = $author->roles[0]; if ( ! is_array( $templates ) && ! empty( $templates ) ) < $templates = locate_template( array( "author-$role.php", $templates ), false ); >elseif ( empty( $templates ) ) < $templates = locate_template( "author-$role.php", false ); >else < $new_template = locate_template( array( "author-$role.php" ) ); if ( ! empty( $new_template ) ) < array_unshift( $templates, $new_template ); >> return $templates; > add_filter( 'author_template', 'author_role_template' );
- Updated 2022-02-15. Added a notice explaining that the template hierarchy is the same for classic and block themes, but that the examples uses .php files and block themes use .html files.