Forum Moderators: phranque

Message Too Old, No Replies

rewrite when URL and directory have nothing in common

         

stressed0ut

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

10+ Year Member



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?

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



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

stressed0ut

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

10+ Year Member



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?

jdMorgan

5:44 am on Dec 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

stressed0ut

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

10+ Year Member



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.

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



> 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

stressed0ut

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

10+ Year Member



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