IPB

Willkommen, Gast ( Anmelden | Registrierung )

 
Reply to this topicStart new topic
> [MOD_REWRITE] Rewrite Rules, Apache 1.3 von http://httpd.apache.org/docs/1.3/misc/rewriteguide.html
Wertung 5 V
jan
Beitrag Jan 20 2007, 22:46 PM
Beitrag #1


Administrator
***

Gruppe: Admin
Beiträge: 552
Mitglied seit: 1-June 05
Wohnort: germany, hassia, bickenbach
Mitglieds-Nr.: 1



From Static to Dynamic

Description:
How can we transform a static page foo.html into a dynamic variantfoo.cgi in a seamless way, i.e. without notice by the browser/user.

Solution:
We just rewrite the URL to the CGI-script and force the correct MIME-type so it gets really run as a CGI-script. This way a request to /~quux/foo.html internally leads to the invokation of /~quux/foo.cgi.

CODE
RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo\.<b>html</b>$  foo.<b>cgi</b>  [T=<b>application/x-httpd-cgi</b>]



On-the-fly Content-Regeneration

Description:
Here comes a really esoteric feature: Dynamically generated but statically served pages, i.e. pages should be delivered as pure static pages (read from the filesystem and just passed through), but they have to be generated dynamically by the webserver if missing. This way you can have CGI-generated pages which are statically served unless one (or a cronjob) removes the static contents. Then the contents gets refreshed.

Solution:
This is done via the following ruleset:

CODE
RewriteCond %{REQUEST_FILENAME}   <b>!-s</b>
RewriteRule ^page\.<b>html</b>$          page.<b>cgi</b>   [T=application/x-httpd-cgi,L]



Here a request to page.html leads to a internal run of a corresponding page.cgi if page.html is still missing or has filesize null. The trick here is that page.cgi is a usual CGI script which (additionally to its STDOUT) writes its output to the file page.html. Once it was run, the server sends out the data of page.html. When the webmaster wants to force a refresh the contents, he just removes page.html (usually done by a cronjob).


Document With Autorefresh

Description:
Wouldn't it be nice while creating a complex webpage if the webbrowser would automatically refresh the page every time we write a new version from within our editor? Impossible?

Solution:
No! We just combine the MIME multipart feature, the webserver NPH feature and the URL manipulation power of mod_rewrite. First, we establish a new URL feature: Adding just :refresh to any URL causes this to be refreshed every time it gets updated on the filesystem.


CODE
RewriteRule   ^(/[uge]/[^/]+/?.*):refresh  /internal/cgi/apache/nph-refresh?f=$1


Now when we reference the URL

CODE
/u/foo/bar/page.html:refresh


this leads to the internal invocation of the URL

CODE
/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html



The only missing part is the NPH-CGI script. Although one would usually say "left as an exercise to the reader" ;-) I will provide this, too.

CODE
#!/sw/bin/perl
##
##  nph-refresh -- NPH/CGI script for auto refreshing pages
##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
##
$| = 1;

#   split the QUERY_STRING variable
@pairs = split(/&/, $ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $name =~ tr/A-Z/a-z/;
     $name = 'QS_' . $name;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     eval "\$name = \"$value\"";
}
$QS_s = 1 if ($QS_s eq '');
$QS_n = 3600 if ($QS_n eq '');
if ($QS_f eq '') {
     print "HTTP/1.0 200 OK\n";
     print "Content-type: text/html\n\n";
     print "<b>ERROR</b>: No file given\n";
     exit(0);
}
if (! -f $QS_f) {
     print "HTTP/1.0 200 OK\n";
     print "Content-type: text/html\n\n";
     print "<b>ERROR</b>: File $QS_f not found\n";
     exit(0);
}

sub print_http_headers_multipart_begin {
     print "HTTP/1.0 200 OK\n";
     $bound = "ThisRandomString12345";
     print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
     &print_http_headers_multipart_next;
}

sub print_http_headers_multipart_next {
     print "\n--$bound\n";
}

sub print_http_headers_multipart_end {
     print "\n--$bound--\n";
}

sub displayhtml {
     local($buffer) = @_;
     $len = length($buffer);
     print "Content-type: text/html\n";
     print "Content-length: $len\n\n";
     print $buffer;
}

sub readfile {
     local($file) = @_;
     local(*FP, $size, $buffer, $bytes);
     ($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
     $size = sprintf("%d", $size);
     open(FP, "<$file");
     $bytes = sysread(FP, $buffer, $size);
     close(FP);
     return $buffer;
}

$buffer = &readfile($QS_f);
&print_http_headers_multipart_begin;
&displayhtml($buffer);

sub mystat {
     local($file) = $_[0];
     local($time);

     ($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file);
     return $mtime;
}

$mtimeL = &mystat($QS_f);
$mtime = $mtime;
for ($n = 0; $n < $QS_n; $n++) {
     while (1) {
         $mtime = &mystat($QS_f);
         if ($mtime ne $mtimeL) {
             $mtimeL = $mtime;
             sleep(2);
             $buffer = &readfile($QS_f);
             &print_http_headers_multipart_next;
             &displayhtml($buffer);
             sleep(5);
             $mtimeL = &mystat($QS_f);
             last;
         }
         sleep($QS_s);
     }
}

&print_http_headers_multipart_end;

exit(0);

##EOF##


--------------------
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
2 Besucher lesen dieses Thema (Gäste: 2 | Anonyme Besucher: 0)
0 Mitglieder:

Collapse

> Similar Topics

  Thema Replies Topic Starter Views Last Action
No New Posts Pinned: Rewrite Rules
abschauen / anpassen
0 jan 13,046 18th November 2006 - 00:35 AM
Last post by: jan


 

- Vereinfachte Darstellung Aktuelles Datum: 13th December 2019 - 02:26 AM