Q:

Redirect failed when setting a custom handler

I have the following code in my custom config file.
<Directory "/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    ErrorDocument 403 "Please disable proxies to view content."
    RewriteEngine On
    RewriteRule ^(.*\.php)s$ $1 [H=application/x-httpd-php-source]
    RewriteRule ^faq/(\w+)/(\d+)/?$ faq.php?code=$1&num=$2 [NC]
</Directory>

Now, when I browse to a webpage like /index.phps or /some/other/dir/and/file.phps, I get a 404 error in the browser and the following is logged in the error log for apache:
[<date>] [:error] [pid 15154:tid 3024079728] [client <IP>] script 'redirect:/request/get_req.php' not found or unable to stat
[<date>] [:error] [pid 15152:tid 2914974576] [client <IP>] script 'redirect:/index.php' not found or unable to stat

What could be causing this issue? Also, please note that the second RewriteRule for faq/.. is working perfectly. I have a few more rewrite rules in per directory basis htaccess files and all of them are working as well.
The server version I'm using is Apache/2.4.9 (Unix).

After following advice from covener and anubhava; I updated my conf file to:
<VirtualHost *:80>
    DocumentRoot "/www"
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    LogLevel warn
</VirtualHost>
<Directory "/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    RewriteEngine On
    # RewriteRule ^(.*\.php)s$ %{DOCUMENT_ROOT}/$1 [H=application/x-httpd-php-source]
    # RewriteRule ^/?(.*\.php)s$ %{DOCUMENT_ROOT}/$1 [H=application/x-httpd-php-source]
    # RewriteRule ^(.*\.php)s$ $1 [H=application/x-httpd-php-source]
    RewriteRule ^(.*\.php)s$ %{DOCUMENT_ROOT}/$1 [H=application/x-httpd-php-source]
    RewriteRule ^faq/(\w+)/(\d+)/?$ faq.php?code=$1&num=$2 [NC]
</Directory>

But the handler for php source is still logging the same error:
[<date>] [:error] [pid 17586:tid 2956938096] [client <IP>] script 'redirect:/index.php' not found or unable to stat

A:

To make it work you need to take out this code snippet out of <Directory> container and place it in <VirtualHost> block:
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>
RewriteEngine On
RewriteRule ^(.*\.php)s$ $1 [H=application/x-httpd-php-source]

Reason why it works in <VirtualHost> block is because rewrite happens in an early phase of
apache processing i.e per-server context. However when it happens in per-dir context, it happens in a late phase and causes the request to be restarted after the substitution. If a substitution is made in per-directory context, a new internal subrequest is issued with the new URL, which restarts processing of the request phases.
See this mod_rewrite tech reference for more details
My VirtualHost entry:
<VirtualHost *:80>
   <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    ErrorDocument 403 "Please disable proxies to view content."
    RewriteEngine On
    RewriteRule ^([^.]+\.php)s$ $1 [H=application/x-httpd-php-source,NC]
</VirtualHost>

