Welcome to WebmasterWorld Guest from 100.25.43.188

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

rewrite when URL and directory have nothing in common

     
7:15 pm on Dec 23, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 23, 2003
posts:5
votes: 0


First off, this looks like a helpful site and after two days of pulling my hair out over this, I'll appreciate any help you guys can offer.

Secondly, the way our Apache install is set up is not of my choosing- we went with a shared hosting platform that uses service drivers between hosted components and uses some awkward ways of file storage and username conventions.

I've recently installed cgiwrap, and my global /cgi-bin is set as /usr/local/apache/cgi-bin. Now I'm trying to allow my virtual hosts to use their cgi-bins (/export/home/site/cgi-bin) with cgiwrap. The catch is that the Virtual Host files are set up like this:

<VirtualHost webserverIP:80>
ServerAdmin siteA@siteA.myISP.net
DocumentRoot /export/home/siteA
ServerName siteA.myISP.net
ServerAlias domain1.net *.domain1.net
Action cgi-wrapper /cgi-bin/cgiwrap/siteA
CustomLog /export/users/siteA/logs/access_log combined
</VirtualHost>

<VirtualHost webserverIP:80>
ServerAdmin siteB@siteB.myISP.net
DocumentRoot /export/home/siteB
ServerName siteB.myISP.net
ServerAlias domain2.net *.domain2.net
Action cgi-wrapper /cgi-bin/cgiwrap/siteB
CustomLog /export/users/siteB/logs/access_log combined
</VirtualHost>

Therefore, I need rewrite to change [domain1.net...] to [domain1.net...] and [domain2.net...] to [domain2.net...] Is such a thing possible without using a mapfile with entries for each virtual host (I have hundreds of virtual hosts)?

UseCanonicalName is set to On, ExecCGI has been allowed for /export/home/*/cgi-bin/, and all virtual hosts are defined in their own conf file (vhosts.conf). Any ideas?

8:35 pm on Dec 23, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


stressed0ut,

Welcome to WebmasterWorld [webmasterworld.com]!

I may be missing some important factor here, but one purpose of mod_rewrite is allow URLs and filepaths to have nothing in common. As long as you don't need to change the domain name, a purely-internal rewrite can be used to redirect any URL to any filepath within the "account's" web space on the server.

I'd encourage you to try some experimentation. The only problem I see is in making it easy to handle your many virtual hosts in an efficient way. The more 'standardized' their URL- and filepath naming conventions are, the easier it will be to accomplish your goal with a minimum or RewriteRules.

For example, in httpd.conf, the following code


RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.
RewriteRule ^/cgi-bin/(.*)$ /cgi-bin/cgiwrap/%2/$1 [L]

will rewrite http://www.domain1.net/cgi-bin/<anyfile> to http://www.domain1.net/cgi-bin/cgiwrap/<hostname>/<anyfile>

Jim

9:08 pm on Dec 23, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 23, 2003
posts:5
votes: 0


Thanks for the reply Jim...I think my real issue here is if there was someway to pull a part of the SERVER_NAME (in this case, just 'siteA' from siteA.myISP.net) into a variable and use that in place of the hostname in the rewrite rule. Is that possible and I've just overlooked it?
5:44 am on Dec 24, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Sure, just use {SERVER_NAME} :

RewriteCond %{SERVER_NAME} ^([^.]*)\.mysisp\.net
RewriteRule ^/cgi-bin/(.*)$ /cgi-bin/cgiwrap/%1/$1 [L]

Ref: Introduction to mod_rewrite [webmasterworld.com] (follow the links in post #1)

Jim

6:46 pm on Dec 24, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 23, 2003
posts:5
votes: 0


My bad...figured out the SERVER_NAME thing after I took a break and got my head cleared up a bit. (DOH!)

The rewrite is working fine, but it looks like I have more of a problem with cgiwrap working after the rewrite. If I comment out the rewrite, cgiwrap works. The cgiwrap log file doesn't even register an attempt when the redirect occurs. I think it's a problem with my directory settings within the httpd.conf file; when a rewrite is involved, is the cgi script running from the original referenced path, or the path returned after the conversion? I installed cgiwrap --with-cgi-bin=cgi-bin.

7:05 pm on Dec 24, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


> is the cgi script running from the original referenced path, or the path returned after the conversion?

That depends on how it's implemented (I don't know), but you are on the right track, as this is a *very* common problem with redirecting when scripts (or even custom error documents) are involved.

Jim

5:17 am on Dec 26, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 23, 2003
posts:5
votes: 0


Got it...needed a [PT] at the end instead of [L]. Thanks for the help.