Solved

mod_rewrite and virtual hosts

Posted on 2006-10-30
9
1,417 Views
Last Modified: 2007-12-19
The System: Apache v2.2.2 on Solaris 9 with mod_rewrite available

This system hosts multiple virtual servers, and these are defined in ./conf/extra/httpd-vhosts.conf

All these virtual hosts are using different Domain Names from the "main" Apache server.

For one specific virtual hosts, I want to re-direct all requests for *any* page under that virtual host to a specific URL in the "main" Apache server.

Let's say the "main" Apache server considers itself "www.mydomain.tld", and the specific virtual host I want to mod_rewrite is "www.deaddomain.tld".

I want to redirect *any* page request that starts out "www.deaddomain.tld" to "www.mydomain.tld/readthis.html"

So...

www.deaddomain.tld --> www.mydomain.tld/readthis.html
www.deaddomain.tld/index.html --> www.mydomain.tld/readthis.html
www.deaddomain.tld/images/title.gif --> www.mydomain.tld/readthis.html
www.deaddomain.tld/guestbook.html --> www.mydomain.tld/readthis.html

The Question:

1) Do the mod_rewrite configuration statements go in ./conf/httpd.conf or in the Virtual Host section of ./conf/extras/httpd-vhosts.conf (I think the latter, but I want to be sure)?

2) What's the proper syntax for the Rewrite* statements?
0
Comment
Question by:PsiCop
  • 4
  • 4
9 Comments
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
RewriteEngine On
RewriteRule .* http://www.mydomain.tld/readthis.html [L,R]

either in .htaccess file in DocumentRoot of the server OR in the VirtualHost section itself
0
 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 100 total points
Comment Utility
That will surely cause an infinite loop?
0
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
Well possibly. Fixed version?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain.tld
RewriteCond %{HTTP_HOST} !^$ //damn http/1.0 clients!
RewriteRule .* http://www.mydomain.tld/readthis.html [L,R]
0
 
LVL 34

Author Comment

by:PsiCop
Comment Utility
Ummm....I'm confused. Shouldn't it be:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.deaddomain.tld
RewriteCond %{HTTP_HOST} !^$ //damn http/1.0 clients!
RewriteRule .* http://www.mydomain.tld/readthis.html [L,R]

I want to redirect www.deaddomain.tld/whatever to www.mydomain.tld/readthis.html - does it matter that the Domain Name is changing to one outside of the deaddomain.tld Virtual Server, or that mydomain.tld is not in a Virtual Server?
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.

 
LVL 43

Accepted Solution

by:
ravenpl earned 400 total points
Comment Utility
The fixed version prevets from redirecting to same virtualserver, so
RewriteCond %{HTTP_HOST} !^www.mydomain.tld //if the request is not to www.mydomain.tld
RewriteCond %{HTTP_HOST} !^$ //and not damn http/1.0 client
RewriteRule .* http://www.mydomain.tld/readthis.html [L,R] //redirect to www.mydomain.tld
//as a result, the www.mydomain.tld gets never redirected
0
 
LVL 34

Author Comment

by:PsiCop
Comment Utility
But I don't want to redirect to the same virtual server.

www.mydomain.tld is the "real" server instance, defined in httpd.conf, not a virtual server.

www.deaddomain.tld is a virtual server, defined in /extras/httpd-vhosts.conf

I want to redirect *from* deaddomain *to* mydomain

I don't understand why www.mydomain.tld is in the RewriteCond statement. Shouldn't www.deaddomain.tld be in there?
0
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
I already explained. we are redirecting to www.mydomain.tld, therefore if request is already to www.mydomain.tld - we wan tto skip it, otherwise redirect. And that what those RewriteConds do: if HTTP_HOST is not www.mydomain.tld AND  HTTP_HOST is not empty -> fire RewriteRule
0
 
LVL 34

Author Comment

by:PsiCop
Comment Utility
Ah! Sorry, missed the !
0
 
LVL 34

Author Comment

by:PsiCop
Comment Utility
OK, that seems to have done it. Thanks! And thanks to HackneyCab for pointing out the loop issue.
0

Featured Post

Highfive Gives IT Their Time Back

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

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

14 Experts available now in Live!

Get 1:1 Help Now