Forum Moderators: phranque

Message Too Old, No Replies

One subdomain serving multiple domains

httpd.conf

         

dervey

1:44 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hello there!

After rampaging on google for a while trying to find the solution, I have found the userbase here to be pretty knowledgeable so here I come with my query!

I have written my httpd.conf in Apache to serve multiple domains, ok no problem.

However, I want to make it so that on every domain my server handles, if the subdomain 'admin' is selected, the system loads a path of my choosing.

I thought about creating a seperate VirtualHost directive in httpd.conf however it only seems to review the directive for the domain in question, which would mean adding a seperate entry for every subdomain.

So, either i'm doing something wrong (likely) or my httpd.conf requires further configuration?

Any help is appreciated :)

jdMorgan

3:17 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're looking for a maintenance-free implemetation at the server config level, then you're up against the fact that hostnames are evaluated from right to left -- That is tld, domain, then subdomain. So I can't think of a neat approach that would let you "wild-card" the tld and domain in a single configuration record, and direct all subdomains of all domains to one DocumentRoot.

The only thing that comes to mind is to point all servers to the same DocumentRoot, and then use mod_rewrite to implement Mass Virtual Hosting, as described in the Apache URL Rewriting Guide [httpd.apache.org]. You could add an exception to the example code, so that your subdomains are handled separately, and pointed to a common directory.

Do be aware that unless you take measures to prevent it, pointing multiple hostnames (subdomains in this case) to the same content will result in duplicate-content issues with the search engines. This can result in ranking power being 'split' across the multiple subdomains, a seemingly-randomly-selected subdomain showing up for each of the common URL-paths (selected by the search engines, and beyond your control), or an actual penalty, depending on the number of duplicated URLs. I suggest that you allow only one (or none) of these common subdomains to be crawled by search robots.

Jim

dervey

3:41 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the response.

The main reason I want to do this is for shared applications, such as QMailAdmin and phpMyAdmin. The applications will never change and search indexing is simply not an issue.

The more I think of it though, it might just be easier to go with a subdirectory rather than a subdomain!

Thanks again.

Paul

dervey

3:50 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Had a further thought about this.

I can set up a wildcard for each domain *.domain.zzz, then use .htaccess in each documentroot directory saying if the url starts admin.#*$!.#*$!, rewrite to url yyy.yyy.yyy.

Is this possible?

jdMorgan

3:53 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd rewrite to internal filepath /admin, not to a URL!

I'd also recommend doing this with an Alias directive in the server config if possible. Remember that .htaccess files are user-accessible...

Jim

dervey

12:55 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Gave this a shot, but doesn't seem to be taking effect;

RewriteCond %{HTTP_HOST} ^([^.]+).example.com$
RewriteCond %{1} !^(www)$
RewriteCond /data/clientapps/%1 -d
RewriteCond %{REQUEST_FILENAME} !^/data/clientapps/
RewriteRule (.*) /data/clientapps/%1/$1 [Last]

This should rewrite *.example.com to /data/clientapps/*

i.e. if someone goes to [mail.example.com...] the documentroot should be loaded as /data/clientapps/mail.

Unfortunately, this doesn't work! Any ideas?

Thanks

Paul

[edited by: jdMorgan at 1:04 pm (utc) on Sep. 18, 2008]
[edit reason] Please use example.com only [/edit]

jdMorgan

1:28 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lots of minor problems:
  • Literal periods in regex patterns must be escaped.
  • If the hostname is end-anchored, the RewriteCond will fail if a period or port number is appended (unusual but valid FQDN formats)
  • Back-references to RewriteCond pattern matches are %1 through %9, not %{1} through %{9}.
    ( "{n}" is a regular expressions quantifier. )
  • Parenthesizing a pattern in a RewriteCond replaces (destroys) all previous RewriteCond back-references.
  • Manually-specified filepaths for exists-checking must include the full server DocumentRoot path
  • Use REQUEST_URI or a back-reference to the RewriteRule pattern for loop prevention.
  • Defer file-exists and directory-exists checks to minimize filesystem calls (performance-affecting)
  • Error in RewriteRule pattern vs. backreference usage consistency (leading slash versus no leading slash).

    Addressing those issues:


    RewriteCond $1 !^data/clientapps/
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteCond %1 !^www$
    RewriteCond %{DOCUMENT_ROOT}/data/clientapps/%1 -d
    RewriteRule ^/(.*)$ /data/clientapps/%1/$1 [L]

    Other readers please note: This code is intended for use in server config files, and will need minor adjustments for use in .htaccess.

    The most troublesome part of rulesets like this has to do with getting the correct path for use in 'exists' checks. You can often see the path the exists check is trying to use in the server error log and/or the rewritelog. If you have any trouble, comment out that line first and re-test.

    Jim

    [edited by: jdMorgan at 1:30 pm (utc) on Sep. 18, 2008]

  •