Apache %{SERVER_NAME} without Hostname?

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

TunaMaxxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wassa_rCommented:
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
TunaMaxxAuthor Commented:
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
Artysystem administratorCommented:
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
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

Artysystem administratorCommented:
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
TunaMaxxAuthor Commented:
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
TunaMaxxAuthor Commented:
Cranked up the points in hopes of inspiring a solution!
0
Artysystem administratorCommented:
> 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
Artysystem administratorCommented:
> 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
Artysystem administratorCommented:
sorry for dupe
0
TunaMaxxAuthor Commented:
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
Artysystem administratorCommented:
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
TunaMaxxAuthor Commented:
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
Artysystem administratorCommented:
> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.