Link to home
Start Free TrialLog in
Avatar of TunaMaxx
TunaMaxx

asked on

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

Avatar of wassa_r
wassa_r
Flag of Australia image

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!
Avatar of TunaMaxx
TunaMaxx

ASKER

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?
Avatar of Arty K
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 :-)

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 :-)

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?
Cranked up the points in hopes of inspiring a solution!
> 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.
> 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.
sorry for dupe
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?
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

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?
ASKER CERTIFIED SOLUTION
Avatar of Arty K
Arty K
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial