Solved

URL rewrites

Posted on 2002-04-03
14
266 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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