Forum Moderators: phranque

Message Too Old, No Replies

Configuring virtual host parameters in .htaccess

         

gregp

1:05 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Hi guys,

First post and I'm not an apache expert by any means so please bear with me! :)

From reading the Apache documentation, my understanding is that the .htaccess file can perform the same roles as the main configuration files (e.g. default-server.conf), but at a user level rather than system admin level. I'm wanting to configure a virtual host in such a way that its behaviour can be controlled in the .htaccess file if this is possible. I don't mind if the basic configuration is done in the vhosts.d directory but I want things like redirects, proxy redirection and so on for that vhost to be controlled in the .htaccess file.

If this helps, here's an example:
Host www.foo.com has a vhost configured (in vhosts.d for example) to point to itself:

<VirtualHost _default_:80>
DocumentRoot /home/html/
ServerName www1.foo.com
</VirtualHost>

In the .htaccess file sitting in /home/html/.htaccess, I then want to be able to start writing specific customizations for that particular host. Obviously both www.foo.com and www1.foo.com will read the same .htaccess file so I need to indicate that the customizations are only for www1.foo.com. However any attempts to start to do this (such as proxy commands) have given me 500 errors.

Can what I'm wanting be achieved, and if so, how?

Thanks!
Greg

jdMorgan

1:24 am on Apr 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that in the documentation for each Apache directive, a small chart appears just after each directive's name. In this chart is an entry named "Context" which describes the context in which that directive may be used.

It is likely that the directives which are causing your 500-Server Errors are not allowed in an .htaccess context.

Only some of the configuration options available at the server config level or at the <VirtualHost> level are available at the .htaccess level. The levels at which particular directives are available depends primarily on their security implications.

Jim

gregp

2:03 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Thanks Jim. I see from that that the <virtualhost> directive can only apply in the server config but the ProxyPass can run in server config, virtual host or directory. I'm making a big guess here that server config means default-server.conf, virtual host means (in our case) vhosts.d/groups.conf and directory means .htaccess - is this correct?

If so then Proxy commands can go in the .htaccess file but they would have to apply to any server which reads that .htaccess file, i.e. there's no way of linking them to a particular virtual host - is this correct? I'm really hoping there's some way we can use the .htaccess file to control the proxying of certain directories by linking them to a particular virtual host.

Thanks,
Greg

jdMorgan

2:10 am on Apr 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, if a directive is allowed in .htaccess, it will say so explicitly.

Jim

gregp

2:15 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Bother, so I guess that means it can't be done. Thanks anyway, drawing board here I come :)

nibb

10:31 am on Apr 24, 2008 (gmt 0)

10+ Year Member



Jim's correct to point out the section of the manual where Directives are explicitly indicated as to where you can use them, but even though I've been using the thing since it started life as the NCSA server (so... about 1992) I still get mixed up whilst considering this sort of thing. There is a fairly easy way to get an understanding over what is likely to be allowed in a .htaccess file (and by that I mean is capable of being allowed - remember some Directives can and probably will be disallowed by server config in the main httpd.conf).

The .htaccess mechanism is there to essentially allow users software level control over domain space, in other words to tweak behaviour as it applies to directories, locations, files. Such objects might reasonably be expected to change either frequently, or to differ across objects within a site.

The main httpd file mechanism allows Admin level control over DOMAINS (and objects), which are not expected to change that frequently. The httpd configs are only consulted on startup or refresh of the main daemon. The .htaccess files are consulted every time Apache looks into a directory, or consults a location or file. [indeed some folks turn off the facility because of the potential overhead to processing it causes, personally I think this is generally negligible].

So, if in doubt, and if you can't get hold of the instance table for the Directive from the manual, ask whether its a setting that you would expect to apply to a domain rather than an object (or indeed software for instance turning on register globals selectively in .htaccess per directory);

For example, setting the ServerName is something you would never expect to do on a per directory basis, it would make no sense to the protocol to have the server swap domain addresses as it traversed a directory tree previously applied to a particular domain as its DocumentRoot.

On the other hand, allowing access to a directory, swapping the default index file for a directory, these are clear examples of a .htaccess level of administration duty.

best

Steve

jdMorgan

2:08 pm on Apr 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A very good point, and a good rule of thumb as well. I'd only suggest substituting the word "hostname" for "domain" in the description above, since a server config file cannot control the DNS-defined domain, but only the HTTP-requested hostname(s) to which it (the server) responds.

Jim