1

I have been looking for the best way to switch my website's root index.html file to a index.php file. My research indicates that doing this carelessly will result in a hit to my rankings in Google and that my best option is to parse the index file as php, noting that this is the only file on the site where such parsing is anticipated to be needed.

I read a great SO thread here: Parse HTML as PHP which was very useful on this subject and I am settled on using the Filesmatch directive to do this in my .htaccess file. However the best response presented there IMHO was one which suggested the following code.

<FilesMatch "^file_name\.html$">
AddType application/x-httpd-php .html
</FilesMatch>

I of course implemented this as:

<FilesMatch "^index\.html$">
AddType application/x-httpd-php .html
</FilesMatch>

While this works just fine for the root index.html page, all the site's sub-directories also have index.html pages and it turns out that they are all getting parsed as php too. For security, I don't want this behaviour. I am not an Apache pro and am getting out of my depth a little here hence asking you guys. Is there a way to stop this and make the root index file the only one targeted by this Filesmatch directive? Thank you.

Community
  • 1
  • 1
Frankie
  • 596
  • 3
  • 24
  • 2
    Unless untrusted users have the ability to write to these other index files, there are no security implications here, and if they do, php is the last of your problems. – Steve Sep 13 '14 at 14:56
  • While that's not the view of many commentators here, I would rather err on the side of caution. In any event, it's messy to have things you don't want parsed as php to start doing so, and I have a very tidy mind, so it's anathema to me. Plus there is a solution to what I'm asking, it's just finding someone who knows it! – Frankie Sep 13 '14 at 15:30
  • Ok, well i expect the answer is to nest your filesmatch node within a DirectoryMatch node – Steve Sep 13 '14 at 15:39
  • Or you could simple redirect internally your root index to the actual index.php file which would not change your browsers URL and would only affect the `domain.com/index.html` and `domain.com/`. – Prix Sep 13 '14 at 17:31
  • @Steve only problem with the nested `directory` or `location` is that he will need access to the server config, so if he only have access to `.htaccess` he won't be able to use it. – Prix Sep 13 '14 at 17:40

1 Answers1

0

Rather than parsing /index.html as a PHP file why not just use 301 (permanent redirect) from /index.html to /index.php using following rule in root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/index\.html [NC]
RewriteRule ^index\.html$ /index.php [L,R=301,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hi @anubhava - and would that only target the root's index.html or would it have a similar effect and cause Apache to look for php versions of all index.html files on the site? I guess I don't see how it discriminates for the root file only. Incidentally what is {THE_REQUEST}? Is that something I should put in literally? – Frankie Sep 13 '14 at 19:09
  • This **will only target root index.html**. That was precisely the reason I suggested `mod_rewrite` to have more power for rewriting stuff. This condition `%{THE_REQUEST} \s/index\.html` will only match `/index.html` i.e. root `index.html` – anubhava Sep 13 '14 at 19:13
  • `THE_REQUEST` variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is `GET /index.php?id=123 HTTP/1.1`. – anubhava Sep 13 '14 at 19:13
  • 1
    thank you for this perfectly good alternative to my request for a more specific Filesmatch directive! It's the answer for me I think - I tested it and it works fine. Thank you! – Frankie Sep 13 '14 at 22:21