?
Solved

Apache %{SERVER_NAME} without Hostname?

Posted on 2008-11-19
13
Medium Priority
?
881 Views
Last Modified: 2012-05-05
The snippet I attached works for what I want it to do, but if I can have it deduce the server's domain name only -- without the hostname -- then I can use it anywhere without modification.

Is it possible to modify this Apache rewrite to remove the dependence on a hard coded domain name? If so, how?

Thanks

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^[^.]*$
  RewriteRule ^(.*)$ "http\:\/\/%{HTTP_HOST}\.domain\.com" [R=301,L]
</IfModule>

Open in new window

0
Comment
Question by:TunaMaxx
  • 7
  • 5
13 Comments
 
LVL 3

Expert Comment

by:wassa_r
ID: 23000002
Have a look at the cheat sheet for htaccess:

http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/

I guess %{SERVER_NAME} will work for you!
0
 

Author Comment

by:TunaMaxx
ID: 23000839
Thanks wassa,

  I have that sheet, or at least another version of it. It's very handy!

  However, %{SERVER_NAME} is returning "hostname.domain.com" for me. I am looking for just "domain.com" though. Any other ideas?
0
 
LVL 27

Expert Comment

by:Nopius
ID: 23001198
The real problem is not how to change to substitute domainname dynamically, but where to take it?

when you call http://shortname, both SERVER_NAME and HOST_NAME will be shortname,
when you call http://longname.com, again SERVER_NAME and HOST_NAME will be longname.com,
so where to take the domain part for substitution? None of server variables contain it (yes, may be ADMIN_ADDRESS, but not for sure).

If you tell me where to get full qualified host name with domain part I'll tell you how to change to it :-)

0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 27

Expert Comment

by:Nopius
ID: 23001206
The real problem is not how to change to substitute domainname dynamically, but where to take it?

when you call http://shortname, both SERVER_NAME and HOST_NAME will be shortname,
when you call http://longname.com, again SERVER_NAME and HOST_NAME will be longname.com,
so where to take the domain part for substitution? None of server variables contain it (yes, may be ADMIN_ADDRESS, but not for sure).

If you tell me where to get full qualified host name with domain part I'll tell you how to change to it :-)

0
 

Author Comment

by:TunaMaxx
ID: 23005993
Although the domains of each physical server might be different from machine to machine, the virtual servers on each will be the same domain as their host. Does that make sense? Ultimately, the setup may look something like this:

Physical server: server1.domain-A.com
Hosting:
 - jim.domain-A.com
 - steve.domain-A.com
 - mary.domain-A.com

Physical server: server2.domain-B.com
Hosting:
 - pam.domain-B.com
 - mike.domain-B.com
 - don.domain-B.com

Physical server: server3.domain-B.com
Hosting:
 - arron.domain-B.com
 - nancy.domain-B.com
 - margaret.domain-B.com

So what needs to be figured out is the domain of the host machine. Is that possible?
0
 

Author Comment

by:TunaMaxx
ID: 23047123
Cranked up the points in hopes of inspiring a solution!
0
 
LVL 27

Expert Comment

by:Nopius
ID: 23055522
> So what needs to be figured out is the domain of the host machine. Is that possible?

Yes, that's possible. The problem is where we would take the physical hostname in a rewrite ruleset?

When you call http://arron/ when asking to server3.domain-B.com, server doesn't know that you assume arron.domain-B.com. You should define some external reference to your physical hostname or better to the upper level domainname.

Normally in Linux the HOSTNAME variable already qualifies full domain name of your machine, check it. Also note, that starting the server with 'service apache start' will loose exported env variables, so start it with 'apachectl start'.

Now the steps to reproduce 'dynamic' domain addition:

1) Stop apache service:
# service httpd stop

2) ensure that you have $HOSTNAME set to FQDN, like 'servername.domainname.com':
# echo $HOSTNAME

3) start apache with apachectl command:
# apachectl start

4) Write the following .htaccess:
RewriteEngine On
PassEnv HOSTNAME
RewriteCond %{ENV:HOSTNAME}/%{HTTP_HOST} ^([^.]*)[.]([^/]*)/([^.]*)$
RewriteRule ^(.*)$ http://%3.%2/$1 [R=301,L,NE]

this should redirect you to the full qualified domain name with domain matching the $HOSTNAME domain part.
0
 
LVL 27

Expert Comment

by:Nopius
ID: 23055528
> So what needs to be figured out is the domain of the host machine. Is that possible?

Yes, that's possible. The problem is where we would take the physical hostname in a rewrite ruleset?

When you call http://arron/ when asking to server3.domain-B.com, server doesn't know that you assume arron.domain-B.com. You should define some external reference to your physical hostname or better to the upper level domainname.

Normally in Linux the HOSTNAME variable already qualifies full domain name of your machine, check it. Also note, that starting the server with 'service apache start' will loose exported env variables, so start it with 'apachectl start'.

Now the steps to reproduce 'dynamic' domain addition:

1) Stop apache service:
# service httpd stop

2) ensure that you have $HOSTNAME set to FQDN, like 'servername.domainname.com':
# echo $HOSTNAME

3) start apache with apachectl command:
# apachectl start

4) Write the following .htaccess:
RewriteEngine On
PassEnv HOSTNAME
RewriteCond %{ENV:HOSTNAME}/%{HTTP_HOST} ^([^.]*)[.]([^/]*)/([^.]*)$
RewriteRule ^(.*)$ http://%3.%2/$1 [R=301,L,NE]

this should redirect you to the full qualified domain name with domain matching the $HOSTNAME domain part.
0
 
LVL 27

Expert Comment

by:Nopius
ID: 23055530
sorry for dupe
0
 

Author Comment

by:TunaMaxx
ID: 23061570
Thanks for the update. I understand the environment variable aspect of your answer, and I had guessed that the solution was going revolve around the server $HOSTNAME somehow. However, I don't catch what is going on in your RewriteCond and RewriteRule. Can you explain?
0
 
LVL 27

Expert Comment

by:Nopius
ID: 23061696
OK.

Lets your env HOSTNAME=server.domain-A.com and your http request is for http://webhost1/subfolder/lala.png

then
RewriteCond %{ENV:HOSTNAME}/%{HTTP_HOST} ^([^.]*)[.]([^/]*)/([^.]*)$

left part is a test string, it becomes 'server.domain-A.com/webhost1'
right part is a test pattern:

^([^.]*)[.] this part matches to 'server.' or to any part of env(HOSTNAME) up to the first dot.
([^/]*)/ matches to 'domain-A.com/' and can be used (without '/') in the next rewrite rule as %2 (2nd parentheses)
([^.]*)$ matches to webhost1 (it matches to any single word without a dot inside) and can be used later as %3 (3rd parentheses)

go ahead:
RewriteRule ^(.*)$ http://%3.%2/$1

left part:
^(.*)$ - this will be matched non-host part of URI, 'subfolder/lala.png' and can be referenced as $1 in the right part

right part:
http://%3.%2/$1 all parameters ha been already defined, so it expands to http://webhost1.domain-A.com/subfolder/lala.png

0
 

Author Comment

by:TunaMaxx
ID: 23062028
Aha! Brilliant explanation. I wasn't even considering any of the 'path' and that's where my confusion came in.

However, unless I am missing something, this no longer works as a solution to my original problem. Sorry if I am blind, but how can this redirect just 'hostname' to hostname.whatevertheserverdomainis.com?
0
 
LVL 27

Accepted Solution

by:
Nopius earned 2000 total points
ID: 23062730
> Sorry if I am blind, but how can this redirect just 'hostname' to hostname.whatevertheserverdomainis.com?

The above rules are exactly for that! When you ask for http://hostname, the HTTP_HOST is set to 'hostname' and the above to rules (RewriteCond and RewriteRule) work. But when you ask for http://hostname.whateverdomain.com, the rules are skipped, because of non-matched RewriteCond.


The only problem as I see is where to get 'whatevertheserverdomainis.com'. I decided to get it from the environment variable HOSTNAME.

To find what is not working, I should know more details. Please provide:
1) echo $HOSTNAME
2) add more logging options to apache config:
 RewriteLog /tmp/rewrite_log
 RewriteLogLevel 7

and restart apache, then try to connect just http://hostname and post the logfile here.
I should know the exact messages in your log if you don't want to publish, send it to my email (you can find it at the end of my profile).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question