Apache Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationApache Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2002, 02:23 AM
pidgin pidgin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 pidgin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to pidgin
Question Apache and sub-domains: mod_rewrite

Hi.
I have the following domains/sub-domains on my Apache.
domain.com
and
dev.domain.com (for development)

I have a rule changing function.domain.com?*-> domain.com/cgi-bin/function.pl?*
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^function\.domain\.com$
RewriteRule ^(.+) /cgi-bin/function.pl [PT,L]
This part is places (as should) in the virtual host definition of domain.com (in httpd.conf)

I want to do the same for dev.domain.com. meaning:
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^function\.dev\.domain\.com$
RewriteRule ^(.+) /cgi-bin/function.pl [PT,L]
I thought I should place it in the dev.domain.com virtual host definition.

BUT - no
when URL is *.dev.domain.com is called - unless its directly dev.domain.com the applying config is that of the domain.com (not dev.domain.com like I though).
Is that the proper behavior?
What can I do to make *.dev.domain.com go thgough the dev.domain.com virtual host.
Coming to think about it - its actually ,ore an Apache question than it is mod_rewrite one, but still...

Any idea?

Reply With Quote
  #2  
Old March 1st, 2002, 03:34 PM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>> RewriteRule ^(.+) /cgi-bin/function.pl [PT,L]

First off, if you want everything in under your REQUEST_URI of / to be handled by /cgi-bin/function.pl, use a internal redirect with a ruleset like so:

RewriteRule ^(.+) /server/path/no/symlink/to/function.pl [T=application/x-httpd-cgi,L]

[PT,L] is usually used to bypass rewriting. For example:

RewriteRule ^/icons/(.+) - [PT,L]

/icons directory is reserved for global use, so don't mess with it.
Your ruleset might not work with the REQUEST_URI is exact /cgi-bin/function.pl, so you would do this right after your RewriteCond like so:

RewriteRule ^/cgi-bin/function\.pl$ - [PT,L]

Next, are you sure your domain have wildcard enabled? If so, just setup a ServerAlias for that. Your whatever.dev.domain.com MUST be DNS resolvable before you can do this.

Reply With Quote
  #3  
Old March 2nd, 2002, 04:24 AM
pidgin pidgin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 pidgin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to pidgin
Hey. (hi freebsd)

What I do now is this... (and it seems to be working)
________________________________

<VirtualHost my.ip.add.ress>
ServerAdmin webmaster@dev.myd.com
DocumentRoot /var/www/html/dev.myd.com
ServerName dev.myd.com
ServerAlias dev.myd.com www.dev.myd.com clicks.dev.myd.com login.dev.myd.com
ErrorLog logs/dev.myd.com-error_log
CustomLog logs/dev.myd.com-access_log common
ScriptAlias /cgi-bin/ /var/www/cgi-bin/dev.myd.com/
ErrorDocument 404 http://dev.myd.com/cgi-bin/traffic/click.pl?36

Rewritelog logs/rewrite.dev.log
RewritelogLevel 9
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^clicks\.dev\.myd\.com$
RewriteRule ^(.+) /cgi-bin/traffic/click.pl [PT,L]

RewriteCond ${lowercase:%{HTTP_HOST}} ^login\.dev\.myd\.com$
RewriteRule ^(.+) /cgi-bin/login.pl [PT,L]

</VirtualHost>
__________________________________

So I am wondering what you meant by
RewriteRule ^(.+) /server/path/no/symlink/to/function.pl [T=application/x-httpd-cgi,L]
and the potential problems the [PT,L].

Can you clarify whats wrong with my above solution?

Thnk you.

Reply With Quote
  #4  
Old March 2nd, 2002, 12:06 PM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
When you want to force REQUEST_URI of anything to be handled by a script, that's equivalent to using ErrorDocument like so:

ErrorDocument 404 /cgi-bin/error.cgi?404

Apache will need further processing to determine the MIME type of the handler - error.cgi.
Like ScriptAlias directive, every file in your scriptalias'ed cgi-bin will have a MIME type of application/x-httpd-cgi enforced.

For your case, [PT] (pass thru) would work but it's no longer the ideal way to deal with a straight internal redirect as:

RewriteRule ^(.+) /cgi-bin/traffic/click.pl [PT,L]

because click.pl is not a static file but a CGI script. Therefore, according to mod_rewrite doc, 'passthrough|PT' (pass through to next handler), you are doing extra processing to determine what handler should handle to process click.pl, and it really is a CGI (or even mod_php script with application/x-httpd-php) which is unnecessary when you can tell Apache, hey, trust me, that click.pl has a application/x-httpd-cgi MIME type.

Further, what pass-thru really means is to tell mod_rewrite, hey this is beyond your duty so just ignore it and let the responsbile person (the handler) to handle it. Like I said in my previous example:

RewriteRule ^/icons/(.+) - [PT,L]

/icons/ directory is reserved for global use because it's handled by mod_alias like so:

Alias /icons/ "/www/icons/"

so it's beyond mod_rewrite's duty and the [PT] is then being used appropriately here.

Reply With Quote
  #5  
Old March 3rd, 2002, 02:10 AM
pidgin pidgin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 pidgin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to pidgin
Hi.
Hey Freebsd. Thx for that explanation. That part is totally clear now.

The only question I still have regarding this is:
How do I make it work so only the
http://login.dev.myd.com/
and
http://login.dev.myd.com
will go to the function, while all the rest (like login.dev.myd.com/images/log.gif) will not be effected.

This is because I only want the login.dev.... to go to the login.pl while images and other redirection should not be redirected to the cgi.

Reply With Quote
  #6  
Old March 3rd, 2002, 06:34 AM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Every request under http://login.dev.myd.com/ will hit your RewriteCond regardless which ruleset you have. You will need to figure out if you really have that many other stuff (images/log.gif) then change the priority of your condition to give you the fastest and most efficient result.
For instance,

RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{HTTP_HOST}} ^login\.dev\.myd\.com$
RewriteRule ^(.+) /server/path/to/cgi-bin/login.pl [T=application/x-httpd-cgi,L]

A request of (1) clicks.dev.myd.com/* as well as (2) login.dev.myd.com/*. To minimize the chance for requesting (1) clicks.dev.myd.com, the rule of thumb is to create ANOTHER unique <VirtualHost> block for login.dev.myd.com so your RewriteCond {HTTP_HOST} is now obsolete and you just need to deal with the (2) part.
Here is an example:
Code:
<VirtualHost my.ip.add.ress>
  ServerAdmin webmaster@dev.myd.com
  DocumentRoot /var/www/html/dev.myd.com
  ServerName login.dev.myd.com
  #ServerAlias dev.myd.com www.dev.myd.com clicks.dev.myd.com login.dev.myd.com (this line is gone)
  ErrorLog logs/login.dev.myd.com-error_log
  CustomLog logs/login.dev.myd.com-access_log common
  ScriptAlias /cgi-bin/ "/var/www/cgi-bin/dev.myd.com/"
  ErrorDocument 404 http://dev.myd.com/cgi-bin/traffic/click.pl?36
  RewriteEngine on
  Rewritelog logs/rewrite.dev.log
  #RewritelogLevel 9 (this is for debugging only)
  #RewriteMap lowercase int:tolower (this is gone, we don't need to deal with HTTP_HOST in 
rewrite condition because it's handled by virtualhost)
  #RewriteCond ${lowercase:%{HTTP_HOST}} ^login\.dev\.myd\.com$
  # if you have a lot of images, add this
  RewriteRule ^/icons/(.+) - [PT,L]
  RewriteRule ^/images/(.+) - [PT,L]
  RewriteCond %{REQUEST_URI} ^/?$ 
  RewriteRule ^(.+) /server/path/to/cgi-bin/login.pl [T=application/x-httpd-cgi,L]
</VirtualHost>

Last edited by freebsd : March 3rd, 2002 at 06:38 AM.

Reply With Quote
  #7  
Old March 4th, 2002, 02:20 AM
pidgin pidgin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 pidgin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to pidgin
hey.
It seems to me like its "little too much" to define a vhost just for functions.
assuming I have/will-have many functions, like submit.dev.myd.com (going to submit.pl), login.dev.myd.com, click.dev.myd.com, signup.dev.myd.com, report.dev.myd.com. each going to his special function.
All I want to do is redirect the "clean" calls (those with only QUERY_STRING - but no REQUEST_URI) to the script while leaving those calls to files (images, scripts, etc... - Tose with non-empty REQUEST_URI) under those aliases free from my intervention.

Isn't it more sensible to do it with mod_rewrite that with vhosts?
maybe it could be done by:

RewriteCond ${lowercase:%{HTTP_HOST}} ^function\.dev\.myd\.com$
RewriteCond %{REQUEST_URI} ??IS_EMPTY??
RewriteRule ^(.+) /server/path/to/cgi-bin/function.pl [T=application/x-httpd-cgi,L]

is there a way to do this ??IS_EMPTY?? thing?
wont it solve my problem efficiently?

BTW:
dont I have to chain the QEURY_STRING to the Rewrited URL? like:
RewriteRule ^(.+) /server/path/to/cgi-bin/function.pl\?%{QUERY_STRING} [T=application/x-httpd-cgi,L]
It somehow works even without - I wonder why?

Reply With Quote
  #8  
Old March 4th, 2002, 03:17 AM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>> It seems to me like its "little too much" to define a vhost just for functions

If you take a close look at your rewrite.log, defining each for a dedicated vhost is not too much, but consumes a lot fewer resource, thus more efficient.

When your RewriteCond ${lowercase:%{HTTP_HOST}} within a <VirtualHost> has too many FQDNs. Even if you only want to match ^function\.dev\.myd\.com$, ^whatever\.dev\.myd\.com$ will still go thru your first condition to match against. When you do the way I suggested previously, you can remove the first condition entirely. Imagine you have a.dev.myd.com to z.dev.myd.com, each request will try to match against your 1st condition, that wastes a lot of resource.

>> is there a way to do this ??IS_EMPTY?? thing?

There is no such thing as an empty REQUEST_URI, it has / at the very least.

>> dont I have to chain the QEURY_STRING to the Rewrited URL?

Yes, but I don't see where QUERY_STRING is coming from when someone requests http://function.dev.myd.com/
You should already know that the default RewriteCond is %{REQUEST_URI}, when you need to switch from that to %{QUERY_STRING}, you need to chain it with [C] so it can carry the rewrite over to the next rule to perform actual rewriting.

Reply With Quote
  #9  
Old March 5th, 2002, 05:22 AM
pidgin pidgin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 pidgin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to pidgin
Hey freebsd.
>> vhost is not too much, but consumes a lot fewer resource, thus more efficient.

makes sense - point taken - done

>> Yes, but I don't see where QUERY_STRING is coming from when someone requests http://function.dev.myd.com/

Its: click.dev.myd.com?123 -> dev.myd.com/cgi-bin/click.pl?123
Thats why the QUERY_STRING (=123) was mentioned.

>> You should already know that the default RewriteCond is %{REQUEST_URI}, when you need to switch from that to %{QUERY_STRING}, you need to chain it with [C] so it can carry the rewrite over to the next rule to perform actual rewriting.

Can you clarify the [C] issue?

>> There is no such thing as an empty REQUEST_URI, it has / at the very least.

What I mean is that / is "empyt".
What I'm doing now (in the login.dev.myd.com vhost) is:
RewriteRule ^/icons/(.+) - [PT,L]
RewriteRule ^/images/(.+) - [PT,L]
RewriteRule ^/cgi-bin/(.+) - [PT,L]
RewriteRule ^(.+) /var/www/cgi-bin/dev.myd.com/login.pl [T=application/x-httpd-cgi,L]
while I only need the login.dev.myd.com to be redirected to /var/www/cgi-bin/dev.myd.com/login.pl i have to state all the preceeding rewrite rules to avoid changind the /icons .images /cgi-bin etc..
is there a way to automatically prevent rewriting of all /[something-that-is-not-empty] so that everything that is http://login.dev.myd.com/*will not be touched while only http://login.dev.myd.com and http://login.dev.myd.com/ will be converted?

Reply With Quote
  #10  
Old March 5th, 2002, 11:16 AM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>> Its: click.dev.myd.com?123 -> dev.myd.com/cgi-bin/click.pl?123

It needs to be click.dev.myd.com/?123 you can't leave out the slash.

>> everything that is http://login.dev.myd.com/*will not be touched

You need at least one RewriteCond such as:

RewriteCond %{REQUEST_URI} !^/[0-9]+$

>> Can you clarify the [C] issue?

You don't need to chain it when there is no HTTP_HOST matching condition.
Here is an example that needs to use [C]:
Code:
RewriteEngine on
RewriteMap      lc              int:tolower
RewriteCond   %{SERVER_NAME}                 ^[^.]+\.domain\.com$ [NC]
RewriteRule   ^/icons/(.+)                  - [PT,L]
RewriteRule   ^(.+)                        %{SERVER_NAME}=$1 [C]
RewriteRule   ^(.*)=(.*)                   ${lc:$1}$2 [C]
RewriteRule   ^([^.]+)\.domain\.com(.*) /var/www/domain.com/$1$2

Like I said, the default condition is to match against REQUEST_URI, in this example, however, I am doing all the rewriting on the SERVER_NAME part, then chain it so my very last rewriterule is doing the REQUEST_URI rewriting. No matter what you do, the final condition must have something to do with REQUEST_URI condition, because that's what Apache sends back to the browser.

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationApache Development > Apache and sub-domains: mod_rewrite


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway