Perl Script Rewrite Maps

Using a single web hosting account to host multiple sites

Perl Script Rewrite Maps

Postby sniper7kills » Thu Apr 10, 2008 1:49 pm

Hello All,
Im currently having some trouble with a rewrite program that was made for me. here is the script along with some information about it.
Code: Select all
You need to create a new MySQL database in the script I called it rewrite. I created a username called rewrite with password rewrite.

# 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
);


The Perl script is as follows:

#!/usr/bin/perl
#
# IT Integrated Business Solutions (c) 2008
#
# 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',
                        'rewrite',
                        'rewrite') || die "Database connection not made: $DBI::errstr";

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

#$in = 'mail.domain.com';
print "Stdin: " . $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";
        }
        $sth->finish();
}

$dbh->disconnect();



The entries in the database consist of the following:

+-----------------+--------------------+------------+
| name            | rewrite            | sort_order |
+-----------------+--------------------+------------+
| mail.domain.com | /var/www/html/mail |         10 |
| www.domain.com  | /var/www/html      |         20 |
| root.domain.com | /user/root/home    |         30 |
| .domain.com     | /var/www/new/html  |         40 |
+-----------------+--------------------+------------+



The sort order is used to determine the first occurrence that matches the criteria. I have used a simple INSTR function available in database to see if the entry in the database can be found in the string that you are passing in. For instance, in the above table, if you had user.domain.com then the last entry .domain.com will match and be returned. This will be the only entry with a fix. However, if you entered mail.domain.com this matches both the first and last entry. The sort order determines which one should be taken first.



Does anyone have any sugestions on how i could get this to work? what i have tried didnt work, and ive been working on it for a while. Thanks
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby richardk » Thu Apr 10, 2008 3:01 pm

What have you done? Why aren't you asking the author?

You need to
  1. Create a new MySQL database called "rewrite" (you can do it with the CREATE DATABASE statement).
  2. Create a database user called rewrite with password rewrite (or you can change the username and or password and change
    Code: Select all
    my $dbh = DBI->connect( 'dbi:mysql:rewrite:localhost',
                            'rewrite',
                            'rewrite') || die "Database connection not made:

    to
    Code: Select all
    my $dbh = DBI->connect( 'dbi:mysql:rewrite:localhost',
                            'your new username',
                            'your new password') || die "Database connection not made:

    ).
  3. Run the CREATE TABLE queries in the rewrite database.
  4. Create a file and put the Perl in it.
  5. You need to put the following in your httpd.conf file
    Code: Select all
    RewriteMap subdomainmap prg:/path/to/perl/script
  6. You need to add data to the database as in the example.
  7. You need to write a mod_rewrite to use it. You probably want something like (also in your httpd.conf file)
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On

    RewriteRule ^(/.*)$ ${subdomainmap:%{HTTP_HOST}|/404/path}$1 [PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sniper7kills » Thu Apr 10, 2008 3:29 pm

No my problem is the rewrite maps, i dont know how to set them up and the author wasnt able to as well.

Using the rewrite rule you provided i get an error and in the logs it rewrites the location as "/var/www/html/var" when it should be "/var/www/html/domain.org"

When i run the perl script in shell i get the right location from the database.
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby richardk » Thu Apr 10, 2008 3:52 pm

Did you replace /404/path with /var/www/html/var? Or is the default from inside the script?

You might need to trim any whitespace around the input in the Perl script.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sniper7kills » Thu Apr 10, 2008 3:57 pm

/var/www/html/var is not anywhere, and as for white space i have no clue on what i would need to delete.
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby sniper7kills » Sat Apr 12, 2008 12:29 pm

sniper7kills wrote:OK, So after some fiddling i got it working better.

in the database it has mail.domain.org -> /var/www/html/mail

when i go to mail.domain.org it goes to /var/www/html/mailmail.domain.org/

here is my rewrite script at the moment:
Code: Select all
rewritemap vhost prg:/etc/httpd/conf/rewrite.pl
Options +FollowSymLinks

RewriteEngine On

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


and i changed
Code: Select all
print "Stdin: " . $in;
to
Code: Select all
print $in;


Sorry For The Double Post i thought i was editing my last one
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Postby richardk » Sat Apr 12, 2008 3:34 pm

Try removing the print line. That might be breaking it as you don't want it to output anything until it's found the right document root (that's the only thing you want it to output). That's probably why you're getting mail.domain.org (the value of $in) returned.

What is the DocumentRoot in your httpd.conf file set to? It should probably be just / so the returned path can be relative to it.

Do you have any <VirtualHost>s or VirtualDocumentRoot directives?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sniper7kills » Sat Apr 12, 2008 4:20 pm

If i take out the first print it no longer works, my browser just waits. as for the document root its the normal /var/www/html But that is not affecting it in anyway (as far as i can tell) and no i dont have any Vhosts.
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

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

If i take out the first print it no longer works, my browser just waits.

Ask the author, i don't know why it's there. The rest of the script is possibly taking too long.

What happens when you go to a different (sub) domain?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sniper7kills » Sat Apr 12, 2008 4:42 pm

if i go from one to another, i have to refresh before it will change the directory. But when i run the script its self it works just fine. if you would like to try it your self please IM me aim: sniper7kills or PM me and i can send you user and domain information to connect.
sniper7kills
 
Posts: 8
Joined: Thu Apr 10, 2008 1:39 pm

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 21 guests

cron