Solved

URL rewrites

Posted on 2002-04-03
14
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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