httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Norman Khine <nor...@khine.net>
Subject Re: [users@httpd] Dynamic RewriteRule based on the URL
Date Thu, 07 Sep 2006 09:10:29 GMT
Norman Khine wrote:
> Boyle Owen wrote:
>   
>>> -----Original Message-----
>>> From: Norman Khine [mailto:norman@khine.net] 
>>> Sent: Wednesday, September 06, 2006 9:23 AM
>>> To: users@httpd.apache.org
>>> Subject: [users@httpd] Dynamic RewriteRule based on the URL
>>>
>>> Hello,
>>> Is it possible to create an Apache Rewrite rule so that the 
>>> depending on
>>> the sub-domain the server receives it is then re-written to a 
>>> different
>>> folder, for example, I have this working but want to reduce it to just
>>> one rule:
>>>
>>> <VirtualHost *:80>
>>>     ServerName  folder_A.domain.tld
>>>     RewriteEngine On
>>>     RewriteRule ^/(.*) http://localhost:9080/folder_A/$1 [P]
>>>     RequestHeader set X-Base-Path folder_A
>>>     ErrorLog       /var/log/apache2/folder_A-error_log
>>>     CustomLog      /var/log/apache2/folder_A-access_log common
>>> </VirtualHost>
>>>
>>> <VirtualHost *:80>
>>>     ServerName  folder_B.domain.tld
>>>     RewriteEngine On
>>>     RewriteRule ^/(.*) http://localhost:9080/folder_B/$1 [P]
>>>     RequestHeader set X-Base-Path folder_B
>>>     ErrorLog       /var/log/apache2/folder_B-error_log
>>>     CustomLog      /var/log/apache2/folder_B-access_log common
>>> </VirtualHost>
>>>     
>>>       
>> Take a look at RewriteCond - this allows you to make conditional
>> redirects and so generate an if-else construct, eg:
>>
>> RewriteCond domainA
>> RewriteRule to folder A
>> RewriteCond domainB
>> RewriteRule to folder B
>>
>> Of course, the rewrite directives then go in the main body of the config
>> so they apply to all VHs. BTW, not sure if this is less or more
>> work/effort than your original solution.. So I guess you're really
>> looking for:
>>
>> RewriteRule to folder(domain)
>>
>> where the destination depends on the value of "domain"... I can't think
>> of an easy way to do this - plough through the examples in
>> http://httpd.apache.org/docs/2.2/misc/rewriteguide.html.
>>
>> Rgds,
>> Owen Boyle
>> Disclaimer: Any disclaimer attached to this message may be ignored. 
>>
>>
>>   
>>     
>>> etc...
>>>
>>>
>>> So is there a nice way to reduce this to only one rule where if a
>>> request is sent to folder_A.domain.tld the RewriteRule, 
>>> RequestHandler,
>>> ErrorLog, CustomLog entries are changed accordingly?
>>>
>>> Perhaps something like in the line of %{SERVER_NAME}:
>>>
>>> <VirtualHost *:80>
>>>     ServerName  %{FOLDER_NAME}.domain.tld
>>>     RewriteEngine On
>>>     RewriteRule ^/(.*) http://localhost:9080/%{FOLDER_NAME}/$1 [P]
>>>     RequestHeader set X-Base-Path %{FOLDER_NAME}
>>>     ErrorLog       /var/log/apache2/%{FOLDER_NAME}-error_log
>>>     CustomLog      /var/log/apache2/%{FOLDER_NAME}-access_log common
>>> </VirtualHost>
>>>
>>>
>>> Would this work and how would you set the %{FOLDER_NAME} if 
>>> it does or is there a different approach?
>>>
>>> Many thanks
>>>
>>>
>>> Norman
>>>
>>>
>>> ---------------------------------------------------------------------
>>> The official User-To-User support forum of the Apache HTTP 
>>> Server Project.
>>> See <URL:http://httpd.apache.org/userslist.html> for more info.
>>> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>>>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
>>> For additional commands, e-mail: users-help@httpd.apache.org
>>>
>>>     
>>>       
>>  
>>  
>> This message is for the named person's use only. It may contain confidential, proprietary
or legally privileged information. No confidentiality or privilege is waived or lost by any
mistransmission. If you receive this message in error, please notify the sender urgently and
then immediately delete the message and any copies of it from your system. Please also immediately
destroy any hardcopies of the message. You must not, directly or indirectly, use, disclose,
distribute, print, or copy any part of this message if you are not the intended recipient.
The sender's company reserves the right to monitor all e-mail communications through their
networks. Any views expressed in this message are those of the individual sender, except where
the message states otherwise and the sender is authorised to state them to be the views of
the sender's company.
>>
>> ---------------------------------------------------------------------
>> The official User-To-User support forum of the Apache HTTP Server Project.
>> See <URL:http://httpd.apache.org/userslist.html> for more info.
>> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
>> For additional commands, e-mail: users-help@httpd.apache.org
>>
>>
>>   
>>     
> Thanks for your reply, I was looking at the
> http://httpd.apache.org/docs/2.2/misc/rewriteguide.html documentation
> and perhaps something like this may work...
>
> RewriteEngine on
> RewriteCond   %{*HTTP_HOST*}                 *[^.]+*\.host\.com$
> RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
> RewriteRule   *([^.]+)*\.host\.com(.*) http://localhost:9080/*$1*$2 [P]
>
> RequestHeader set X-Base-Path %{*HTTP_HOST*}
>
> #Not sure if the logs entry is correct
>
> ErrorLog       /var/log/apache2/%{*HTTP_HOST*}-error_log
> CustomLog      /var/log/apache2/%{*HTTP_HOST*}-access_log common
>
> Cheers
>
> Norman
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>   
Hello again,
I am still having problems with this and am not sure how to tackle this
from a most effective point of view.

I have setup a wild card DNS entry so that *.mydomain.tld points to an
apache web server.

This works as expected.

On my apache conf file I have a rule that:

<VirtualHost *:80>
    ServerName  folder.domain.tld
    RewriteEngine On
    RewriteRule ^/(.*) http://localhost:9080/folder/$1 [P]
    RequestHeader set X-Base-Path folder
    ErrorLog       /var/log/apache2/folder-error_log
    CustomLog      /var/log/apache2/folder-access_log common
</VirtualHost>


This also works fine without problem.

BUT, I want to have just one rule and use some special apache magic to
change this on the fly ;)

I tried the examples in the
http://httpd.apache.org/docs/2.2/misc/rewriteguide.html and specifically
the example of the Virtual User Host but I cannot get it to work as I am
still getting the apache main page, rather than the page in the directory.

Perhaps my conf file is wrong, here it is:

[1] <VirtualHost *:80>

[2]    ServerName  domain.tld
[3]    RewriteEngine on
[4]    RewriteCond   %{*HTTP_HOST*}    *[^.]+*\.domain\.tld$
[5]    RewriteRule   ^(.+)             %{HTTP_HOST}$1                        [C]
[6]    RewriteRule   *([^.]+)*\.domain\.tld(.*) http://localhost:9080/*$1*$2 [P]

[7]    RequestHeader set X-Base-Path %{*HTTP_HOST*}

[8]    ErrorLog       /var/log/apache2/all-error_log
[9]    CustomLog      /var/log/apache2/all-access_log common

[0] </VirtualHost>


I don't understand line [2], as the server gets the request http://folder.domain.tld as this
points to an A record IP address Apache will return the default page and it stops, how do
I inject the %{*HTTP_HOST*} value or is there some magic I am missing and don't see?

Assuming that there is, Apache then switches the RewriteEngine on [3] and takes everything
before domain.tld - what is the meaning of [^.]+ ? then the [C] means continue to the next
RewriteRule which in my case needs to rewrite to an internal server and Proxy this i.e. [P],
but this does not work for me either ;'(

Line [7] I need to set the X-Base-Path to the folder name.

I would like to avoid using vhost.map or any scripting but I am open to suggestions if there
is no other way ;) 

By the way, I don't have the www.'folder'.domain.tld in my URL - maybe this is the problem,
but don't see why?

Cheers

Norman


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message