homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Setting up 3 domains in one conf file
getting weird fluctuations

 9:52 am on Jul 30, 2009 (gmt 0)


I'm running a dedicated server, and have access to the httpd.conf files.

I currently have two domains running on there, call it example.com and example.es - the latter simply redirects to example.com

What's weird is that the redirect for example.es sometimes works, and sometimes doesn't - it instead points to the DocRoot.

Here is the conf file:



ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@example.com

<Directory "/home/htdocs">
AllowOverride All
Options FollowSymLinks
Order allow,deny
Allow from all
DocumentRoot "/home/htdocs"

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com [R=301]

# 301 redirect other domains over to .com
RewriteCond %{HTTP_HOST} ^example.es [NC]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^ http://www.example.com/ [L,R=301]


Have I set this up correctly? I want to add a couple more domains that just do a 301 redirect to the example.com, and am concerned that even my first redirect is not functioning as expected :-(

A second question: I want to add another domain, running a separate site.

I tried adding a seperate VirtualHost like this:


ServerName www.example2.com
ServerAlias example2.com
ServerAdmin webmaster@example2.com

<Directory "/home/htdocs2">
AllowOverride All
Options FollowSymLinks
Order allow,deny
Allow from all
DocumentRoot "/home/htdocs2"


But strangely, all that does is also point to the DocRoot of the FIRST virtual host (i.e. /home/htdocs instead of /home/htdocs2).

I'm a little out of my depth here, and would really appreciate any feedback on what I'm doing wrong. Many thanks!



 10:55 am on Jul 30, 2009 (gmt 0)

Hmm, as so often happens, posting in the forum led me to the right answer just because it made me think more logically about the problem!

The issue with the fluctuating redirect was that the rule I had above was only redirecting the non-www url...ahem

It also turned out to be more logical to add a virtual host just for that redirect, like so:

# ============ Redirecting example.es =============

ServerName www.example.es
ServerAlias example.es
ServerAdmin webmaster@example.es

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]



So all good! (Mods, how can I add [SOLVED] to the subject line?)


 2:07 pm on Jul 30, 2009 (gmt 0)

We hope that most threads here are eventually [solved]... :)

However, that doesn't always mean there's nothing left to discuss.

Note that you could use a single mod_alias directive instead of the far-more-complex mod_rewrite for this purpose:
Redirect 301 / http://www.example.com/

Also, in future, when using regular-expressions patterns as in your previous RewriteCond %{HTTP_HOST} directives, be sure to escape all literal periods and other characters which are interpreted as regex 'tokens':
RewriteCond %{HTTP_HOST} ^exampl[b]e\.e[/b]s [NC]

Remember that in regular expressions, a period is taken as meaning "match any single character" and in some circumstances, failing to escape such characters can result in an exploitable vulnerability.



 2:56 pm on Jul 30, 2009 (gmt 0)

Thanks Jim, I've made the suggested amendment - you're right, it's a much more succinct approach! And thanks for the tip on using "." in my regular expressions :-)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved