Solved

mod_rewrite and virtual hosts

Posted on 2006-10-30
9
1,426 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
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, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

724 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