Solved

URL rewrites

Posted on 2002-04-03
14
265 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need assistance modifying php script 3 40
Post form data to PHP then to 3rd party site 19 38
XAMPP 14 42
Apache,  not running php on top level 14 59
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 …
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

16 Experts available now in Live!

Get 1:1 Help Now