Solved

URL rewrites

Posted on 2002-04-03
14
264 Views
Last Modified: 2010-03-04
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!
0
Comment
Question by:scotsmen
  • 8
  • 6
14 Comments
 
LVL 15

Expert Comment

by:samri
ID: 6916989
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,
0
 

Author Comment

by:scotsmen
ID: 6919424
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.)
0
 
LVL 15

Expert Comment

by:samri
ID: 6919761

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.
0
 

Author Comment

by:scotsmen
ID: 6919782
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?
0
 
LVL 15

Expert Comment

by:samri
ID: 6920189
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.

0
 
LVL 15

Expert Comment

by:samri
ID: 6920191
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
0
 

Author Comment

by:scotsmen
ID: 6920197
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?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Accepted Solution

by:
samri earned 200 total points
ID: 6920304
I think the following rule should do just what you are looking at;

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

However, we need to cosider the fact about client getting to connect to the server itself.  That is where the VirtualHost directive is in place so that the Apache knows that there is a anything that has to do with the newdomain should be treated there.

Another possible options would be implementing the following RewriteRule in the global server config.

-------------------
RewriteEngine on
RewriteCond   %{REMOTE_HOST}  ^olddomain\.com$
RewriteRule   ^(/~.+)         http://www.newdomain.com/$1 [R,L]
-------------------

Or another option is to create a different Virtual Host that DocumentRoot to the Samepath.

I personally would recomment the VirtualHost options since it should be much more efficient.

Depending on either approach, it is best for you to give each option a try.  Then decide on the most comfortable approach.

cheers.
 


0
 

Author Comment

by:scotsmen
ID: 6923987
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.
0
 
LVL 15

Expert Comment

by:samri
ID: 6924470
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
0
 

Author Comment

by:scotsmen
ID: 6924479
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!
0
 

Author Comment

by:scotsmen
ID: 6924486
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?
0
 
LVL 15

Expert Comment

by:samri
ID: 6924487
No problem.

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

cheers,
0
 
LVL 15

Expert Comment

by:samri
ID: 6924514
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now