In one of my sites, www.whatever.com I prepared .htaccess file with rewrite rules that permanently redirect [r=301,nc] whatever.com to whatever.com/whatever/whatever.htm and then replace .htm files with corresponding .php files without 301 redirection, so that php would be completely hidden from the visitors.

I am pretty sure I tested it and it was working fine a few months ago. When I tried it today, I saw it showing php files instead, and all the links are, therefore, broken.

I expect the first set of rules 301 redirect www.whatever.com into www.whatever.com/whatever/whatever.htm, and it does it when I remove the last rule.

The last rule should convert resulting www.whatever.com/whatever/whatever.htm into
www.whatever.com/whatever.php/whatever.php, but since there is no 301 redirect in the last rule the visitors should still see www.whatever.com/whatever/whatever.htm in the browser address line! The problem is that they see www.whatever.com/whatever.php/whatever.php instead.

The last rule works correctly by itself, however.

Again, I am pretty sure I tested it and it was working fine a few months ago.

It did work after I added [NC,R=301,L] as you recommended. However, I still do not understand the logic there. After the first rule produced www.whatever.com/whatever/whatever.htm I do want the second rule to convert it to corresponding php. So, why do I mark the first rule with L?

It certainly looks better than mine, but unfortunately I have hard time understaning it. Here is what I do not understand:

1. In mine $1 stands for www.whatever.com that preceeds the directory name. Why do you think it is unnecessary?
2. What is the advantage of using (/.+\.) instead of my /(.*)\. I do understand the reason of replacing * with +, but not combining (.+) with preceeding slash and the following dot.
3. What is the reason for QSA? (It seems to work without it too!)

1. In mine $1 stands for www.whatever.com that preceeds the directory name. Why do you think it is unnecessary?

Your $1 does not contain anything. The RewriteRule only matches the part of the URL after the directory the .htaccess file is in. So if you have a .htaccess file in /foo and go to example.com/foo/bar, the RewriteRule only matches bar.

2. What is the advantage of using (/.+\.) instead of my /(.*)\. I do understand the reason of replacing * with +, but not combining (.+) with preceeding slash and the following dot.

Just so you don't have to write it out again.

3. What is the reason for QSA? (It seems to work without it too!)

Query String Append:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule wrote:'qsappend|QSA' (query string append) This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.

If you go to /whatever/a.htm?abc=def it would rewrite to /whatever.php/a.php?abc=def.