In part 2 you are saying if the host header starts example or m.example then redirect to
www.example.com.
I'd ask this:
Is the physical directory for
www.example.com different to the physical directory where the .htaccess file is held?
If it is different, but under the root where .htaccess file with the rule is held, it may be inheriting from the root .htaccess file. At one point in time the default behaviour was that an .htaccess in the root folder will affect all its subfolder, unless overwritten by subfolder-specific .htaccess files. However, that appears to have changed and inheritance is no longer the default behaviour. It is set by the directive RewriteOptions inherit | InheritBefore so check these are not set.
If your redirect is into the same root directory you will have a loop. .htaccess runs every time the directory is entered. The 'last rule' stanza applies on a per iteration basis. Your host header is not rewritten by your rule - which forms you logic test - so the condition will always be true, taking you into a never ending loop. The fix is probably to direct into another directory.
Depending on the access you have to the server, you can enable logging for mod_rewrite with something like this in the vhost declaration/httpd.conf:
Code:
RewriteLogLevel 3
RewriteLog /var/log/apache2/rewrite.log
This will show you exactly what is happening.
Personally I would code for this in a scripting language like php or asp and just include it at the top of each page, rather than let mod_rewrite do it. It's a bit 'clunkly' for the job you are trying to do.