[mdlug] Apache rewrite not working

Mark Montague markmont at umich.edu
Fri Apr 29 21:26:19 EDT 2011


  On April 29, 2011 8:45 , "Michael ORourke" <mrorourke at earthlink.net> 
wrote:
>      ServerName www.example.com
>      ServerAlias www.newsite.com
>      RewriteEngine on
>      RewriteCond %{HTTP_HOST} ^example\.com [OR]
>      RewriteCond %(HTTP_HOST) ^www\.newsite\.com [OR]
>      RewriteCond %(HTTP_HOST) ^newsite\.com
>      RewriteRule ^(.*)$ http://www.example.com [R=permanent,L]
>
> The DNS records for both the domains point to the same public address.
> Site Newsite should redirect to www.example.com
> The first rule for adding www. to example.com does work but neither of the
> newsite rules does.
> I have the same format working for a different site and different virtual
> host, but the redirect for www.newsite.com does not work.

First, you have parentheses instead of curly brackets around HTTP_HOST 
in the second and third RewriteCond lines.  Fix this.

Also note that HTTP_HOST is the value of the Host header in the HTTP 
request, and that this will not be set by web browsers or other clients 
using HTTP 1.0; only by clients using HTTP 1.1 or above.  The only good 
way around this that I know of is to use by-IP virtual hosts for each 
separate fully qualified domain name that a client might use to access 
your web server.  But you can skip this if you know for sure that no 
client will use HTTP 1.0 or if you're OK with those clients not being 
redirected and always remember that this can happen.

Next, try getting rid of the regular expression pattern matching and do 
string matching instead; this is simpler and may be more reliable:

     RewriteCond %{HTTP_HOST} =example.com [OR]
     RewriteCond %{HTTP_HOST} =www.newsite.com [OR]
     RewriteCond %{HTTP_HOST} =newsite.com
     RewriteRule . http://www.example.com [R=permanent,L]


If that does not work, use the RewriteLog directive to enable logging of 
what mod_rewrite is doing, and add a RewriteLogLevel directive that sets 
the logging level to 9.  Then try one of the requests that are not being 
properly rewritten, and see if the reason is obvious from the rewrite 
log messages; if not, post the messages here so we can take a look at them.

(Note that I've changed the pattern for the RewriteRule above to 
something shorter and more efficient that also avoids the overhead of 
setting up a capture, since you're not using $1 in the substitution part 
of the rule.)

I hope this helps.

--
   Mark Montague
   mark at catseye.org




More information about the mdlug mailing list