: Redirect all index.html pages to subdirectory? I have an old site ported from WordPress to basic HTML (don't ask why) and every page is ending in the subdirectory/index.html. This is causing
I have an old site ported from WordPress to basic HTML (don't ask why) and every page is ending in the subdirectory/index.html.
This is causing some funky indexing issues, so I wanted to 301 all the index.html to its parent subdirectory.
So for instance, I would like my pages:
example.com/index.html to 301 to example.com/
example.com/service/index.html to 301 to example.com/service/
example.com/about-us/index.html to 301 to example.com/about-us
etc, etc.
More posts by @Hamaas447
1 Comments
Sorted by latest first Latest Oldest Best
First, a couple of assumptions:
All your internal links must already point to the non-index.html URL, eg. example.com/service/. (However, since you state that "every page is ending in the subdirectory/index.html" and you are experiencing some "funky indexing issues, I wonder if this is the case?)
You are using .htaccess on Apache.
Try something like the following near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index.html
RewriteRule (.*)index.html$ / [R=301,L]
UPDATE: The check against THE_REQUEST in the RewriteCond directive ensures that only direct requests from the client, that contain index.html, get redirected. THE_REQUEST server variable contains the initial request header and does not get modified when the URL is rewritten. This avoids a redirect loop when mod_dir later issues a subrequest for index.html (the DirectoryIndex document).
However, this could be "simplified" further. The RewriteCond directive could be omitted if using the NS (nosubreq) flag on the RewriteRule directive. The NS flag prevents the rule being processed on "subrequests". As mentioned above, when mod_dir rewrites the URL, it is considered a subrequest. So, by simply including the NS flag, we again avoid a redirect loop. So, the above directives could be replaced with a single RewriteRule directive:
RewriteRule (.*)index.php$ / [NS,R=301,L]
If you also have a canonical www to non-www redirect then it may be optimal to include the above rule first and specify the canonical absolute domain in the RewriteRule substitution (thus avoiding a potential double redirect). For example:
:
RewriteRule (.*)index.html$ example.com/ [R=301,L]
example.com/about-us/index.html to 301 to example.com/about-us
I assume the missing trailing slash on your last example was a typo? The above rule includes a trailing slash (which is required). Omitting the trailing slash introduces additional complexity.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2025 All Rights reserved.