Solved

mod_rewrite and virtual hosts

Posted on 2006-10-30
9
1,425 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
[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
  • 4
  • 4
9 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 17840357
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
ID: 17841320
That will surely cause an infinite loop?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17841344
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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 34

Author Comment

by:PsiCop
ID: 17841777
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
 
LVL 43

Accepted Solution

by:
ravenpl earned 400 total points
ID: 17841833
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
ID: 17843448
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
ID: 17843581
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
ID: 17844043
Ah! Sorry, missed the !
0
 
LVL 34

Author Comment

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

Featured Post

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.

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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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