DevHeads.net

Apache 2.2 URL Rewriting Guide From Static to Dynamic 404 Not Found script not found or unable to stat: redirect:/printenv.pl

Apache users:

I'm trying to learn how to use the Apache rewrite module to redirect
incoming hits for virtual *.html pages to Perl CGI scripts using the
information provided in the Apache 2.2 manual "URL Rewriting Guide"
chapter "From Static to Dynamic" section:

<a href="http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#static-to-dynamic" title="http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#static-to-dynamic">http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#static-to-dy...</a>

For my first test, I am attempting to have hits for the page:

<a href="http://bluefish.holgerdanske.com/printenv.html" title="http://bluefish.holgerdanske.com/printenv.html">http://bluefish.holgerdanske.com/printenv.html</a>

invoke the script at URL:

<a href="http://bluefish.holgerdanske.com/printenv.pl" title="http://bluefish.holgerdanske.com/printenv.pl">http://bluefish.holgerdanske.com/printenv.pl</a>

which resides in file:

/home/dpchrist/bluefish/public_html/printenv.pl

When I invoke the script via its direct URL, everything works:

<a href="http://bluefish.holgerdanske.com/printenv.pl" title="http://bluefish.holgerdanske.com/printenv.pl">http://bluefish.holgerdanske.com/printenv.pl</a>

But when I request the URL that is supposed to be rewritten:

<a href="http://bluefish.holgerdanske.com/printenv.html" title="http://bluefish.holgerdanske.com/printenv.html">http://bluefish.holgerdanske.com/printenv.html</a>

I see:

404 Not Found

and the Apache error log says:

script not found or unable to stat: redirect:/printenv.pl

Also, I have attempted to enable the rewrite log and set the target
directory mode to 0777, but no log file is written (?).

Please see console session, below.

Any suggestions?

TIA,

David

2011-02-19 22:18:15 dpchrist@p43400e ~/bluefish
$ cat /etc/debian_version
6.0

2011-02-19 21:18:20 dpchrist@p43400e ~/bluefish
$ sudo apache2ctl -V
Server version: Apache/2.2.16 (Debian)
Server built: Jan 1 2011 21:57:31
Server's Module Magic Number: 20051115:24
Server loaded: APR 1.4.2, APR-Util 1.3.9
Compiled using: APR 1.4.2, APR-Util 1.3.9
Architecture: 32-bit
Server MPM: Worker
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/worker"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"

2011-02-19 21:18:23 dpchrist@p43400e ~/bluefish
$ cat /etc/apache2/apache2.conf | grep '.' | egrep -v '^#'
LockFile ${APACHE_LOCK_DIR}/accept.lock
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy all
</Files>
DefaultType text/plain
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
Include mods-enabled/*.load
Include mods-enabled/*.conf
Include httpd.conf
Include ports.conf
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\"
\"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\""
combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
Include conf.d/
Include sites-enabled/

2011-02-19 21:25:14 dpchrist@p43400e ~/bluefish
$ l -w 64 /etc/apache2/mods-enabled/
./ autoindex.conf@ mime.load@
../ autoindex.load@ negotiation.conf@
alias.conf@ cgid.conf@ negotiation.load@
alias.load@ cgid.load@ reqtimeout.conf@
auth_basic.load@ deflate.conf@ reqtimeout.load@
authn_file.load@ deflate.load@ rewrite.load@
authz_default.load@ dir.conf@ setenvif.conf@
authz_groupfile.load@ dir.load@ setenvif.load@
authz_host.load@ env.load@ status.conf@
authz_user.load@ mime.conf@ status.load@

2011-02-19 21:54:51 dpchrist@p43400e ~/bluefish
$ cat /etc/apache2/sites-enabled/bluefish.holgerdanske.com
# $Id: bluefish.holgerdanske.com,v 1.5 2011-02-20 05:54:48 dpchrist Exp $
<VirtualHost *:80>
ServerName bluefish.holgerdanske.com
DocumentRoot /home/dpchrist/bluefish/public_html
RewriteLog /home/dpchrist/bluefish/log/rewrite.log
ScriptAlias /cgi-bin/ /home/dpchrist/bluefish/cgi-bin/
</VirtualHost>

2011-02-19 21:54:59 dpchrist@p43400e ~/bluefish
$ sudo apache2ctl configtest
Syntax OK

2011-02-19 21:55:05 dpchrist@p43400e ~/bluefish
$ sudo apache2ctl graceful

2011-02-19 21:55:46 dpchrist@p43400e ~/bluefish
$ l -w 64 public_html/
./ .htaccess favicon.png madewithbluefish-w3cstyle1.png
../ CVS/ index.html printenv.pl@

2011-02-19 21:57:14 dpchrist@p43400e ~/bluefish
$ cat public_html/.htaccess
# $Id: .htaccess,v 1.2 2011-02-20 05:47:55 dpchrist Exp $
Options +ExecCGI
AddHandler cgi-script pl
RewriteEngine on
RewriteBase /
RewriteRule ^printenv\.html$ printenv.pl [H=cgi-script]

2011-02-19 21:59:48 dpchrist@p43400e ~/bluefish
$ cat public_html/printenv.pl
#! /usr/bin/perl
# $Id: printenv.pl,v 1.3 2011-02-20 05:47:55 dpchrist Exp $
print "Content-Type: text/plain\n\n";
foreach my $k (sort keys %ENV) {
print $k, ' ', $ENV{$k}, "\n";
}

2011-02-19 22:00:41 dpchrist@p43400e ~/bluefish
$ wget -O - <a href="http://bluefish.holgerdanske.com/printenv.pl" title="http://bluefish.holgerdanske.com/printenv.pl">http://bluefish.holgerdanske.com/printenv.pl</a>
--2011-02-19 22:01:00-- <a href="http://bluefish.holgerdanske.com/printenv.pl" title="http://bluefish.holgerdanske.com/printenv.pl">http://bluefish.holgerdanske.com/printenv.pl</a>
Resolving bluefish.holgerdanske.com... 192.168.0.34
Connecting to bluefish.holgerdanske.com|192.168.0.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: `STDOUT'

[<=> ] 0 --.-K/s
DOCUMENT_ROOT /home/dpchrist/bluefish/public_html
GATEWAY_INTERFACE CGI/1.1
HTTP_ACCEPT */*
HTTP_CONNECTION Keep-Alive
HTTP_HOST bluefish.holgerdanske.com
HTTP_USER_AGENT Wget/1.12 (linux-gnu)
PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
QUERY_STRING
REMOTE_ADDR 192.168.0.34
REMOTE_PORT 55402
REQUEST_METHOD GET
REQUEST_URI /printenv.pl
SCRIPT_FILENAME /home/dpchrist/bluefish/public_html/printenv.pl
SCRIPT_NAME /printenv.pl
SERVER_ADDR 192.168.0.34
SERVER_ADMIN [no address given]
SERVER_NAME bluefish.holgerdanske.com
SERVER_PORT 80
SERVER_PROTOCOL HTTP/1.0
SERVER_SIGNATURE <address>Apache/2.2.16 (Debian) Server at
bluefish.holgerdanske.com Port 80</address>

SERVER_SOFTWARE Apache/2.2.16 (Debian)
TZ America/Los_Angeles
[ <=> ] 765 --.-K/s in
0s

2011-02-19 22:01:00 (61.2 MB/s) - written to stdout [765]

2011-02-19 22:01:00 dpchrist@p43400e ~/bluefish
$ wget -O - <a href="http://bluefish.holgerdanske.com/printenv.html" title="http://bluefish.holgerdanske.com/printenv.html">http://bluefish.holgerdanske.com/printenv.html</a>
--2011-02-19 22:01:14-- <a href="http://bluefish.holgerdanske.com/printenv.html" title="http://bluefish.holgerdanske.com/printenv.html">http://bluefish.holgerdanske.com/printenv.html</a>
Resolving bluefish.holgerdanske.com... 192.168.0.34
Connecting to bluefish.holgerdanske.com|192.168.0.34|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2011-02-19 22:01:14 ERROR 404: Not Found.

2011-02-19 22:01:14 dpchrist@p43400e ~/bluefish
$ sudo tail -n 3 /var/log/apache2/error.log
[Sat Feb 19 22:00:41 2011] [notice] SIGUSR1 received. Doing graceful
restart
[Sat Feb 19 22:00:41 2011] [notice] Apache/2.2.16 (Debian) configured --
resuming normal operations
[Sat Feb 19 22:01:14 2011] [error] [client 192.168.0.34] script not
found or unable to stat: redirect:/printenv.pl

2011-02-19 22:31:26 dpchrist@p43400e ~/bluefish
$ ll -d ~/.
drwxr-xr-x 53 dpchrist dpchrist 4096 Feb 19 21:54 /home/dpchrist/./

2011-02-19 22:34:37 dpchrist@p43400e ~/bluefish
$ ll -d ~/bluefish/
drwxr-xr-x 7 dpchrist dpchrist 4096 Feb 19 22:25 /home/dpchrist/bluefish//

2011-02-19 22:34:44 dpchrist@p43400e ~/bluefish
$ ll -d ~/bluefish/log
drwxrwxrwx 3 dpchrist dpchrist 4096 Feb 19 22:31
/home/dpchrist/bluefish/log/

2011-02-19 22:34:47 dpchrist@p43400e ~/bluefish
$ l log/
./ ../ .cvsignore .exists CVS/

Comments

Re: Apache 2.2 URL Rewriting Guide From Static to

By =?utf-8?Q?Igor_... at 02/20/2011 - 16:37

Why? (<a href="http://bash.org/?866112" title="http://bash.org/?866112">http://bash.org/?866112</a>)

<a href="http://blag.esotericsystems.at/igor/says/no" title="http://blag.esotericsystems.at/igor/says/no">http://blag.esotericsystems.at/igor/says/no</a>

You do not, NEVER EVER chmod something 0777. There is no place for
0777 on any server. Not even a test server. There is a place for
1777 and that place is /tmp and /var/tmp.

Generally, mod_rewrite is debugged using RewriteLog.
<a href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog" title="http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog">http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog</a>

But see below.

Aha.. might want to check out:
<a href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog" title="http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog">http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog</a>
later on...

[snip]
How many people, I wonder, have mod_authz_groupfile loaded
without using it?

Why when you have full access to the config do you put your
rewrite rules in .htaccess?

Please their documentation for what they are good for:
<a href="http://httpd.apache.org/docs/current/howto/htaccess.html" title="http://httpd.apache.org/docs/current/howto/htaccess.html">http://httpd.apache.org/docs/current/howto/htaccess.html</a>

Finally, I fail to see why you're even using rewrite for that thing?
You could just name your files .html, make them executable.

Options +ExecCGI would only pick up those that are exectuable.
Of course that's not "transparent" but if you're looking for
transparency, why not just *drop* .html *and* .pl from the *Request*
and have Options +MultiViews take care of serving the right file?

See my last mail to users@ which, too, suggests avoiding mod_rewrite:)
<a href="http://www.mail-archive.com/ ... at httpd dot apache.org/msg46255.html" title="http://www.mail-archive.com/ ... at httpd dot apache.org/msg46255.html">http://www.mail-archive.com/ ... at httpd dot apache.org/msg46255.html</a>

i

Re: Apache 2.2 URL Rewriting Guide From Static to

By David Christensen at 02/21/2011 - 15:22

Apache users:

I've boiled the example down, attempted to clarify, and re-posted it on
the debian-user mailing list:

<a href="http://lists.debian.org/debian-user/2011/02/msg02171.html" title="http://lists.debian.org/debian-user/2011/02/msg02171.html">http://lists.debian.org/debian-user/2011/02/msg02171.html</a>

David

Re: Apache 2.2 URL Rewriting Guide From Static to

By David Christensen at 02/20/2011 - 18:20

On 02/20/2011 12:37 PM, Igor Galić wrote:

As a matter of fact, I *do* know how to operate a jackhammer. Do you
need your demons let out? ;-)

Oh, isn't he cute!

I was expecting Apache to run as www-data:

2011-02-20 13:35:44 dpchrist@p43400e ~/bluefish
$ sudo grep -r APACHE_RUN_USER /etc
/etc/apache2/envvars:export APACHE_RUN_USER=www-data
/etc/apache2/apache2.conf:User ${APACHE_RUN_USER}

But, Apache seems to be running as root:

2011-02-20 14:00:57 dpchrist@p43400e ~/bluefish
$ chmod 0755 log

2011-02-20 14:06:11 dpchrist@p43400e ~/bluefish
$ sudo rm log/rewrite.log

2011-02-20 14:06:18 dpchrist@p43400e ~/bluefish
$ sudo apache2ctl graceful

2011-02-20 14:06:25 dpchrist@p43400e ~/bluefish
$ ll log/rewrite.log
-rw-r--r-- 1 root root 0 Feb 20 14:06 log/rewrite.log

So, you're right -- mode 0777 wasn't necessary in this case.

(Perhaps a Debian bug report is in order...)

Unfortunately, it's empty. (Another Debian bug report?)

587,243, give or take. ;-)

Because I don't have full access to the config file on shared hosting
servers.

Yes, I've read that: past, present, and future. :-)

So, if I want HTML pages to be generated by Perl scripts named *.html,
then should I write HTML pages named *.pl to generate my Perl scripts?
Do I then pipe the output of wget to perl -e? But, how do I feed that
into Apache to generate the HTML page? Perhaps I should really use
*.html for Bash scripts? ;-)

<a href="http://httpd.apache.org/docs/2.2/mod/core.html#options" title="http://httpd.apache.org/docs/2.2/mod/core.html#options">http://httpd.apache.org/docs/2.2/mod/core.html#options</a>

<a href="http://httpd.apache.org/docs/2.2/content-negotiation.html" title="http://httpd.apache.org/docs/2.2/content-negotiation.html">http://httpd.apache.org/docs/2.2/content-negotiation.html</a>

<a href="http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html" title="http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html">http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html</a>

Interesting.

<a href="http://wiki.apache.org/httpd/WhenNotToUseRewrite" title="http://wiki.apache.org/httpd/WhenNotToUseRewrite">http://wiki.apache.org/httpd/WhenNotToUseRewrite</a>

"These examples assume the RewriteRules are not placed in a directory
context (i.e., not in a .htaccess file nor in a <Directory> section)."

For this test, the rewrite rules are in .htaccess.

<a href="http://httpd.apache.org/docs/trunk/rewrite/avoid.html" title="http://httpd.apache.org/docs/trunk/rewrite/avoid.html">http://httpd.apache.org/docs/trunk/rewrite/avoid.html</a>

"The most common situation in which mod_rewrite is the right tool is
when the very best solution requires access to the server configuration
files, and you don't have that access. Some configuration directives are
only available in the server configuration file. So if you are in a
hosting situation where you only have .htaccess files to work with, you
may need to resort to mod_rewrite."

Shared hosting without access to the server configuration files is the
use-case I am testing.

David