Forum Moderators: phranque

Message Too Old, No Replies

domain conditional redirection

internal redirection using HTTP_HOST

         

RufusM

12:33 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



I have a website, [maindomain.org...] which has a number of other domain names pointing to it (e.g. www.domainb.com). I would like anyone who uses the url www.domainb.com to be taken to [maindomain.org...] rather than to the actual root homepage.

To do this I have set up the following rewrite rule:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.maindomain\.org
RewriteRule ^(.*)$ [maindomain.org...] [L]

This is working fine, but there are two additional requirements wanted:

1. I would prefer that the www.domainb.com url remain visible in the browser address bar, rather than changing to the [maindomain.org...] address. I tried to do this as an internal redirect by writing the RewriteRule as:

RewriteRule ^(.*)$ /subfoldera/subfolderb/ [L]

but this just results in the browser waiting when the url www.domainb.com is used, and it never resolves to anything. There must be some logical inconsistency I am missing.

2. The existing RewriteRule pattern sends all paths to the /subfoldera/subfolderb/ index page. I would prefer that the path was retained, e.g. so that www.domainb.com/sub/file.shtml was sent to www.maindomain.org/subfoldera/subfolderb/sub/file.shtml. I know how to do this with Redirect, but not using a RewriteRule. Can it be done with Rewrite, or can the RewriteCond be passed through to mod_alias for a Redirect? (I have assumed that there is no way of conditioning for domain names with RedirectMatch.)

The server is a Linux box running Apache 1.3, but I don't have access to the httpd.conf. The above is being handled in a .htaccess context.

I would be grateful for any solutions people can suggest. In the course of researching the above I have come across this forum and found it to be an excellent resource, thankyou.

jdMorgan

2:15 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RufusM,

Welcome to WebmasterWorld!

From your description and looking at you code, it's likely that your code is looping. For practical purposes, mod_rewrite in .htaccess appears to call itself, and will loop as long as the RewriteRule patterns still match. This is because the results of mod_rewrite must be passed back to httpd.conf to check against the restrictions and rewrites in that file, and then control will be passed back to .htaccess in the newly-rewritten directory path for a check against its restrictions and rewrites. So, mod_rewrite in .htaccess can give the appearance of being recursive.

To fix this, you have to explicitly prevent this looping by making sure that the rule won't be invoked on a second pass through the code:


RewriteEngine on
# Rewrite to subdirectory if not main domain
RewriteCond %{HTTP_HOST} !^www\.maindomain\.org
# Prevent recursive rewrites if we've already done the rewrite
RewriteCond %{REQUEST_URI} !^/subfoldera/subfolderb/
# Else rewrite to requested resource in subdirectory
RewriteRule (.*) /subfoldera/subfolderb/$1 [L]

Jim

RufusM

2:50 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



Jim, thanks for this, it does the trick on both fronts.

One unusual problem has resulted. Once a www.domainb.com url is rewritten like this users are taken to pages which contain links written relative to the document root (e.g. as "/subfolder/file.html"). These pages similarly call a stylesheet using a root relative reference. The server is reporting that it is unable to find these resources, and the page displays without styles. My understanding is that the server is trying to find www.domainb.com/resource.css rather than www.maindomain.org/resource.css.

My understanding of this is that, while there is a DNS entry for www.domainb.com pointing to the same IP address as www.maindomain.org, the server config itself has no alias for the www.domainb.com, so while it can find resources using the RewriteRule via the HTTP_HOST value supplied by the browser, it cannot resolve these internal requests. Have I understood this correctly, and if so, is the fix to add a server alias into the Virtual Host config?

Many thanks again for your help.

jdMorgan

4:02 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That sounds plausible.

Another solution would be to add RewriteConds to by-pass the rewriting for images, stylesheets, external JavaScript files, etc. if the resultant mixed-domain-URL results would be acceptable to you.

Jim

RufusM

11:08 am on Mar 7, 2005 (gmt 0)

10+ Year Member



Thanks Jim for this alternative suggestion.

Just for the record, I think what we were ideally after was a Named Virtual Host (see the Apache documentation). It turns out that the webserver administrators don't provide these, so we've settled for a simple redirect of the index page only, specifying this explicitly in the rewrite rule, so that other requests go through normally. For a small site we can add each file in explicitly and get the same effect as a generic rule, though this gets to be a maintenance issue if the site is large. For what we need at the moment though this is the best mix of simplest and least effort. And in the meantime I learned something, so thanks for the help.