Perl Script Rewrite Maps

Using a single web hosting account to host multiple sites

Postby richardk » Sat Apr 12, 2008 4:50 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap vhost prg:/etc/httpd/conf/rewrite.pl

RewriteRule .* ${vhost:%{HTTP_HOST}|/var/www/html/docs}%{REQUEST_URI} [PT,L]

with and without the print statement.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sniper7kills » Sat Apr 12, 2008 5:03 pm

with out the print statement it still just waits, and with it no change

I dont know if this will help but here is the rewrite log:
Code: Select all
192.168.1.43 - - [12/Apr/2008:21:00:43 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) init rewrite engine with requested uri /
192.168.1.43 - - [12/Apr/2008:21:00:43 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (3) applying pattern '.*' to uri '/'
192.168.1.43 - - [12/Apr/2008:21:00:43 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (5) map lookup OK: map=vhost key=www.sniper7kills.org -> val=www.sniper7kills.org
192.168.1.43 - - [12/Apr/2008:21:00:43 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) rewrite / -> www.sniper7kills.org/
192.168.1.43 - - [12/Apr/2008:21:00:43 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) forcing 'www.sniper7kills.org/' to get passed through to next API URI-to-filename handler
192.168.1.43 - - [12/Apr/2008:21:00:45 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) init rewrite engine with requested uri /
192.168.1.43 - - [12/Apr/2008:21:00:45 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (3) applying pattern '.*' to uri '/'
192.168.1.43 - - [12/Apr/2008:21:00:45 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (5) map lookup OK: map=vhost key=www.sniper7kills.org -> val=/var/www/html/sniper7kills.orgwww.sniper7kills.org
192.168.1.43 - - [12/Apr/2008:21:00:45 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) rewrite / -> /var/www/html/sniper7kills.orgwww.sniper7kills.org/
192.168.1.43 - - [12/Apr/2008:21:00:45 --0400] [www.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) forcing '/var/www/html/sniper7kills.orgwww.sniper7kills.org/' to get passed through to next API URI-to-filename handler
192.168.1.43 - - [12/Apr/2008:21:00:52 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) init rewrite engine with requested uri /
192.168.1.43 - - [12/Apr/2008:21:00:52 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (3) applying pattern '.*' to uri '/'
192.168.1.43 - - [12/Apr/2008:21:00:52 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (5) map lookup OK: map=vhost key=mail.sniper7kills.org -> val=/var/www/html/sniper7kills.orgmail.sniper7kills.org
192.168.1.43 - - [12/Apr/2008:21:00:52 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) rewrite / -> /var/www/html/sniper7kills.orgmail.sniper7kills.org/
192.168.1.43 - - [12/Apr/2008:21:00:52 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) forcing '/var/www/html/sniper7kills.orgmail.sniper7kills.org/' to get passed through to next API URI-to-filename handler
192.168.1.43 - - [12/Apr/2008:21:00:56 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) init rewrite engine with requested uri /
192.168.1.43 - - [12/Apr/2008:21:00:56 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (3) applying pattern '.*' to uri '/'
192.168.1.43 - - [12/Apr/2008:21:00:56 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (5) map lookup OK: map=vhost key=mail.sniper7kills.org -> val=/var/www/html/mailmail.sniper7kills.org
192.168.1.43 - - [12/Apr/2008:21:00:56 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) rewrite / -> /var/www/html/mailmail.sniper7kills.org/
192.168.1.43 - - [12/Apr/2008:21:00:56 --0400] [mail.sniper7kills.org/sid#9f78e78][rid#a282f60/initial] (2) forcing '/var/www/html/mailmail.sniper7kills.org/' to get passed through to next API URI-to-filename handler
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby sniper7kills » Mon Apr 14, 2008 2:36 am

OK so i got it to work!

1) Set Document Root to "/"
2) Use this Perl Script:
Code: Select all
#!/usr/bin/perl
#
# IT Integrated Business Solutions (c) 2008
# http://www.it-iss.com
#
# Author: Ronan Cashell
# There is no warranty that goes with this script.
#
# For this to work we must connect to the database and await STDIN. The input is then cross checked against
# the database for a valid entry. An example:
#
# If entry provided is mail.domain.com we need to rewrite to /var/www/html/mail
# In a one to one match this is easy. However, wildcards are also supposed to work
# with this utility. So if mail.domain.com is entered then we need to cross check against
# the database to see the first match in the database against this.
#
# So lets imagine in the database we have *.domain.com this means that anything requesting
# www.domain.com/mail.domain.com ... should have the rewrite. A sort order is included to
# configure the first match that it picks up.
#
# the MySQL instr function will help check if the entry exists in the database
use strict;
use DBI;

$| = 1;

my $dbh = DBI->connect( 'dbi:mysql:rewrite:localhost',
                        'USER',
                        'PASSWORD') || die "Database connection not made: $DBI::errstr";

my $in;
while ($in = <STDIN>) {

#$in = 'mail.domain.com';
#print "" . $in;
#print $in;
        my $sql = "SELECT name, rewrite FROM rewrite WHERE INSTR('" . $in . "', name) > 0 ORDER BY sort_order ASC";
        my $sth = $dbh->prepare( $sql );
        $sth->execute();

        my( $name, $rewrite);
        $sth->bind_columns( undef, \$name, \$rewrite );
        if($sth->fetch()) {
      print $rewrite."\n";
        } else {
                # Important to have a default if there are no matches. Otherwise Apache will hang.
                print "/var/www/html\n";
        }
        $sth->finish();
print $_;
}
$dbh->disconnect();



3) Use This ReWrite Rule:
Code: Select all
options +Indexes +FollowSymlinks


RewriteEngine On

RewriteMap vhost prg:/etc/httpd/conf/rewrite.pl

rewriteCond %{REQUEST_URI} !^/icons/
rewriteCond %{REQUEST_URI} !^/cgi-bin/

RewriteRule ^(.*)$ ${vhost:%{HTTP_HOST}}$1 [PT,L,NS]

EDIT The last one works but it didnt do what i needed so this works better

Code: Select all
options +ALL
RewriteEngine On
RewriteMap vhost prg:/etc/httpd/conf/rewrite.pl
# deal with aliases as above
RewriteCond  %{REQUEST_URI}               !^/icons/
RewriteCond  %{REQUEST_URI}               !^/cgi-bin/
RewriteCond  ${%{HTTP_HOST}}  ^(.+)$
# this does the file-based remap
RewriteCond  ${vhost:%1}                  ^(/.*)$
RewriteRule  ^/(.*)$                      %1/$1
RewriteCond  %{REQUEST_URI}               ^/cgi-bin/
RewriteCond  ${{HTTP_HOST}}  ^(.+)$
RewriteCond  ${vhost:%1}                  ^(/.*)$
RewriteRule  ^/(.*)$                      %1/cgi-bin/$1


4)And Dont Forget the mysql tables:
Code: Select all
# Creates a blank database
CREATE DATABASE rewrite;

# Creates the user with the permissions
GRANT all on rewrite.* to locahost@rewrite identified by ‘rewrite’;

# Create the table
CREATE TABLE rewrite (
        name varchar(100),
        rewrite varchar(100),
        sort_order int
);
[/b]
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby richardk » Mon Apr 14, 2008 2:31 pm

I think you should be able to shorten it to
Code: Select all
Options All

RewriteEngine On

RewriteMap vhost prg:/etc/httpd/conf/rewrite.pl

RewriteRule  !^/(icons|cgi-bin)(/.*)?$ ${vhost:%{HTTP_HOST}}%{REQUEST_URI} [NC,PT,L]
RewriteRule  ^(/cgi-bin(/.*)?)$ ${vhost:%{HTTP_HOST}}$1 [NC,PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 23 guests

cron