Thursday, January 27, 2011

Need to have the home page redirect to a different document root with apache.

I want to have anything other than www.example.com and example.com directed to a different root directory.

so example.com and

example.com/page will be going to two seperate roots.

i need to do this so i can have the home page directed elsewhere while under development.

here is what my vhost for the site looks like.

ServerName www.example.com

ServerAlias *.example.com



DocumentRoot /var/www/example   

  • Try this on your .htaccess file at /var/www/example:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
    RewriteRule (.*) %2/other_folder/$1 [L]
    

    Which would redirect anything but www.example.com and example.com to another folder.

    OR

    To actually have a different DocumentRoot you would need to create a new VirtualHost, you could try for example:

    <virtualhost *:80>
        ServerName www.example.com
        ServerAlias example.com
        DocumentRoot /var/www/example
    </virtualhost>
    
    <virtualhost *:80>
        ServerName anything.example.com
        ServerAlias *.example.com
        DocumentRoot /var/www/another_example_root_folder
    </virtualhost>
    

    So your first virtualhost would take only with and without www and the second anything else.

    : I tried the first method you mentioned with no luck i ended up getting this fixed by doing it in the php thanks anyways
    Prix : @user52177 Yes you can do that with a php aswell, anyway glad you got it working you might want to add your php code on a new answer and mark it as the solution to your question as it may also help others with the same problem. http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work/5235#5235
    From Prix

0 comments:

Post a Comment