Link to home
Start Free TrialLog in
Avatar of scotsmen
scotsmen

asked on

URL rewrites

I'm running Apache on Win32.  I would like to setup a subdomain that would forward to a URL on a different site.  For instance, forums.mydomain.com would point to www.mydomain.com/forums.  How do I go about doing this?  It seems that the rewrite mod only allows you to rewrite URL within the same domain.

Also, how would I go about using Apache to point a domain (sub.domainone.com) to a different one (sub.domaintwo.com)?

Thanks!
Avatar of samri
samri
Flag of Malaysia image

Check this config.  It should work.  And make sure the forums.mydomain.com is defined in the DNS (either A or CNAME record).

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.

<VirtualHost *>
  ServerAdmin webmaster@mydomain.com
  DocumentRoot /usr/local/apache/htdocs
  ServerName www.mydomain.com
</VirtualHost>

<VirtualHost *>
  ServerAdmin webmaster@mydomain.com
  DocumentRoot /usr/local/apache/otherdomain
  ServerName forums.mydomain.com

  RewriteEngine on
  RewriteRule   ^/*  http://www.mydomain.com/forums/$1  [R,L]
</VirtualHost>

More examples and information could be found here:
http://httpd.apache.org/docs/misc/rewriteguide.html


The other option is to use Apache's Proxy capability (with VirtualHost).

try this one out, and let us know.  Should you need to get more information the 2nd option, let us know.

cheers,
Avatar of scotsmen
scotsmen

ASKER

Hrm.  I get an error when trying to use the directive "RewriteEngine On".  Is this not included in the Windows distribution by default?  I'm running 1.3.23.  Not sure how I'd go about compiling it in on my Windows system.  (I REALLY need to switch to Linux soon.)

Could you check whether you have the mod_rewrite.so in the following directory c:\Program Files\Apache Group\Apache\modules>

The one I have is :
Server version: Apache/1.3.22 (Win32)
Server built:   Oct 11 2001 07:36:10

and it has mod_rewrite.so included.

Next thing to check is your httpd.conf file.  Make sure the line
---
LoadModule rewrite_module modules/mod_rewrite.so
---
is not commented.

If is still missing, ie. the modules is not there, you might need to get a newer version of Apache.

http://www.apache.org/dist/httpd/binaries/

Give it a shot.
Perfect!  Thanks so much.  A few clarifying questions if I may (I'll be sure to raise the points, if that makes any difference.)  I've been looking over the syntax for mod_rewrite, but I'm still not clear on this.  How could I have it so that if somebody were typing in this:
test.domain.com/specific/page.html
it would then get forwarded to this:
test.differentdomain.com/specific/page.html

How does the URL value get carried through?
Rewrite rules is a bit tricky.  Personally, I'm not much good myself.

The apache module docs has a depth discussion on such;
http://httpd.apache.org/docs/mod/mod_rewrite.html

And there are a few examples presented in the following page;
http://httpd.apache.org/docs/misc/rewriteguide.html

It is very useful.

For your specific scenario, assuming the you had the following VirtualHost defined;

<VirtualHost *>
 ServerAdmin webmaster@testdomain.com
 DocumentRoot /usr/local/apache/testdomain
 ServerName test.domain.com
 RewriteEngine  on
 RewriteRule    ^/specific/page\.html$ http://test.differentdomain.com/specific/page.html

</VirtualHost>

I tested this config, and it should work.  give it a shot.

Leave some space between the "$" and "http".  Somehow the formatting screws up.  It's for readability.

RewriteRule    ^/specific/page\.html$          http://test.differentdomain.com/specific/page.html
I'm sorry, I should have clarified.  Do you know what the rewrite rule would be for just changing the domain name?  So that one rule would take care of any request that is going to the old domain, automatically substituting the new domain name but keeping the same path information?
ASKER CERTIFIED SOLUTION
Avatar of samri
samri
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hrm.  It doesn't seem to be working.  I have this in httpd.conf:
RewriteEngine on
RewriteCond %{REMOTE_HOST} ^test\.olddomain\.com$
RewriteRule ^(/~.+) http://test.newdomain.com/$1 [R,L]

I decided to keep it outside of the VirtualHost directive.  Ideally, with this in the global config, then I shouldn't need to keep a VH up for the old domain, correct?  Anyway, unfortunately the redirection doesn't work.  I tried getting rid of the old VH thinking that the child process was listening to requests instead of redirecting them, but that didn't help.  Any ideas? I really appreciate your help.
I would presume that you had test.newdomain.com defined as one of your VirtualHost.  Try removing all of your VirtualHost directive (comment them out, since it's for testing), and try.  Since you had VirtualHost defined, in most cases, if any match for VirtualHost failed, the request will fall to the first defined VH (the default).  By placing the rewrite rules in this VH, it should do the job.


Another docs from Apache (another alternatives maybe);
http://httpd.apache.org/docs/misc/howto.html#redirect
Ah.  OK.  I got it.  I modified the rule by getting rid of the tilda.  That, plus having the directive in the virtual host, seemed to fix the problem.  I'm going to test it out for a day with some different scenarios, and then I'll close the question.  Thanks so much!
One more question:

Do I have to keep the VirtualHost containers?  It seems kind of silly to have a virtual host created which contains three lines (Servername, RewriteEngine, RewriteRule.)  Testing this shows that the VH defaults over to the first VH (as you've already mentioned.)  Is there no way around this?  Surely having extra VHs creates unnecessary overhead?
No problem.

We'll see if any of the proposed solutions works for you.

cheers,
Unless you need to do any VirtualHost'ing, then you will have to keep the VH, and remember to define the 1st VH for your own/default server.  Other than that, you can remove it.

I will have to agree on the fact that additional VH will tax some resources.  Unfortunately, I cannot quantify the resources the VH will consume.

I would rather see the whole setup from manageability.  By segregating each VH (for example), it is easier to manage iin the future, should you want to expand, and provide such services.  If we go ahead, and put that in the Global server config, somehow, when adding VH in the future, we might end-up going thru the same problem.  Another gray-hair maybe.

Just a thought.