Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Please point me in the right direction.

I am using .htaccess to set-up domains on my server.

I need to domain.com to access files from /domain.com/* folder. If a file does not exist, load /domain.com/index.php and domain.com/images/image.jpg should load file: /domain.com/images/image.jpg

One thing is that it should load everything without sending 302 "file moved" notices to browser.

Thanks for your help!

share|improve this question
Don't use .htaccess files for this. I don't even think you can - if I remember correctly, the necessary directives aren't allowed in .htaccess files. –  David Z Jul 8 '09 at 22:10
This isn't a job for rewrite rules. It's a job for virtual hosts. (In fact, rewrite rules shouldn't be the first thing you think of when configuring Apache, anyway.) –  staticsan Jul 8 '09 at 23:22
I need to configure Zend framework to work with my other scripts. I would like to apply their example for multiple domains: framework.zend.com/wiki/display/ZFDEV/… –  Alex L Jul 13 '09 at 18:18

3 Answers 3

up vote 3 down vote accepted

If you have a lot of domains, or even if you have only a few domains but they all have more or less the same configuration, this is the kind of situation that dynamic mass virtual hosting is made for. Something like this would go in your httpd.conf file (or in a file that's included in httpd.conf):

UseCanonicalName Off
VirtualDocumentRoot /var/www/%0/htdocs
ErrorLog /var/www/logs/error_log
CustomLog /var/www/logs/access_log combined

I'd also suggest

DirectoryIndex index.php

to load the index.php file in a directory if the client doesn't specify a filename.

share|improve this answer

You should create a virtual host for every single domain you'd like to map to a file path, and specify the DocumentRoot of each vhost to the actual path on the filesystem. Then, you'll want to be sure you have a DEFAULT vhost, so that all other requests for domains that are not setup, will use the DEFAULT vhost. Lastly, you'll want to create a custom 404 error handler, so that if the file does not exist that is being requested, it will serve up alternative content. You will need to create a php called custom_404.php that displays a generic message and your /images/image.png.

# Default Domain (Undefined domains will fall back to this)
<VirtualHost *:80>
    ServerAlias www.defaultdomain.com
    DocumentRoot /var/www/default/htdocs
    ErrorDocument 404 /custom_404.php
    ErrorLog /var/www/default/logs/error_log
    CustomLog /var/www/default/logs/access_log combined

# domain1.com
<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /var/www/domain1.com/htdocs
    ErrorDocument 404 /custom_404.php
    ErrorLog /var/www/domain1.com/logs/error_log
    CustomLog /var/www/domain1.com/logs/access_log combined

# domain2.com
<VirtualHost *:80>
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/domain2.com/htdocs
    ErrorDocument 404 /custom_404.php
    ErrorLog /var/www/domain2.com/logs/error_log
    CustomLog /var/www/domain2.com/logs/access_log combined

Hope this helps!


share|improve this answer

You need to use virtual hosts to organize this. Here is a sample virtual host file.

<VirtualHost *:80>
    ServerName example.net
    ServerAlias www.example.net
    ServerAdmin traustitj@example.com
    DocumentRoot /var/www/vhosts/www.example.net/html
    ErrorLog /var/www/vhosts/www.example.net/logs/errors.log
    Customlog /var/www/vhosts/www.example.net/logs/access.log combined


Place this file inside /etc/httpd/conf.d/ And call it for example, vhost_example.net.conf

And then add inside /etc/httpd/conf/httpd.conf in for example the last line

Include /etc/httpd/conf.d/vhost_example.net.conf

(or just Include /etc/httpd/conf.d/*.conf ) And in httpd.conf file, remember to make this line uncommented

NameVirtualHost *:80

share|improve this answer
Thanks but I wanted to know how it's done using rewriting in .htaccess. Also, that rewrite rule has to load index.php in case of a 404 –  Alex L Jul 8 '09 at 22:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.