Solved

How Do I Redirect All But Root Page To Another Domain?

Posted on 2008-10-10
9
229 Views
Last Modified: 2012-05-05
I need to redirect all non-root page requests to a VirtualHost to another domain.

For example:

1) if www.domain1.com/page1 is requested, it needs to be redirected to www.domain2.com/page1
2) if www.domain1.com/ is requested, no redirect should occur, a page will be served

Simple enough.

www.domain1.com's DirectoryIndex is landing_page.php however, so another complication arises. I don't want the browser to have www.domain1.com/landing_page.php displayed in their browser, so I need an additional redirect from www.domain1.com/landing_page.php to www.domain1.com/.

So:
3) if www.domain1.com/landing_page.php is requested, a redirect should occur to www.domain1.com/ 

I simply don't understand what I'm doing with RewriteCond and RewriteRule, as you'll probably see in the following edited version of my VirtualHost in httpd.conf.

I've tried every variation of my current setup that I can think of, and the result's the same, it doesn't work properly.

Right now, according to the log, it appears that my problem is that when / is requested, internally apache is replacing / with landing_page.php, which I assume is failing the first condition as the headers are not external, then passes the second condition and forwards to www.domain2.com/landing_page.php.

Again, if www.domain1.com/ is requested, no redirects should occur, and the browser should display www.domain1.com in the address bar. If www.domain1.com/landing_page.php is requested, the browser should display www.domain1.com in the browser. If www.domain1.com/page_1.php is requested, a 301 redirect should switch them to www.domain2.com/page_1.php.

I'm sure I'm doing something very stupid. I'm frustrated and my head hurts :/
Any help is very appreciated. I can provide a log if necessary, will just need to change the names to protect the innocent.
<VirtualHost 1.2.3.4>
  DirectoryIndex landing_page.php
  ServerName www.domain1.com
 
  # redirect www.domain1.com/landing_page.php to www.domain1.com/
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*landing_page\.php\ HTTP/
  RewriteRule ^(.*)landing_page\.php$ $1 [R=301,L]
 
  # ensure that the request isn't the internal page request (landing_page.php)
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\ HTTP/
  # if the request isn't for the home page..
  RewriteCond %{REQUEST_URI} !^/$
  # redirect to the equivalent domain2 page
  RewriteRule ^(.*) http://www.domain2\.com$1 [R=301,L]     
</VirtualHost>

Open in new window

0
Comment
Question by:LongShotBuddy
[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
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22698051
RewriteCond %{THE_REQUEST} ^[/]*landing_page\.php
RewriteRule ^(.*)landing_page\.php$ / [R=301,L]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://www.domain2\.com/$1 [R=301,L]
0
 

Author Comment

by:LongShotBuddy
ID: 22698213
Thanks for your response! Much appreciated.

It's doing the same thing it was doing with (at least one version of) my messed up code however, when I go to www.domain1.com/, it's redirecting me to www.domain2.com/landing_page.php, where it shouldn't do a redirect if the root page is requested.  

The masked rewrite log is attached, thanks ahoffmann :)
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#81794ac/initial] (2) init rewrite engine with requested uri /
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#81794ac/initial] (3) applying pattern '^(.*)landing_page\.php$' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#81794ac/initial] (3) applying pattern '^(.*)' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#81794ac/initial] (4) RewriteCond: input='/' pattern='!^/$' => not-matched
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#81794ac/initial] (1) pass through /
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817b4bc/subreq] (2) init rewrite engine with requested uri /landing_page.php
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817b4bc/subreq] (1) pass through /landing_page.php
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (2) init rewrite engine with requested uri /landing_page.php
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (3) applying pattern '^(.*)landing_page\.php$' to uri '/landing_page.php'
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (4) RewriteCond: input='GET / HTTP/1.1' pattern='^[/]*landing_page\.php' => not-matched
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (3) applying pattern '^(.*)' to uri '/landing_page.php'
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (4) RewriteCond: input='/landing_page.php' pattern='!^/$' => matched
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (2) rewrite /landing_page.php -> http://www.domain2.com/landing_page.php
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (2) explicitly forcing redirect with http://www.domain2.com/landing_page.php
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (1) escaping http://www.domain2.com/landing_page.php for redirect
123.234.345.456 - - [12/Oct/2008:11:01:37 -0600] [www.domain1.com/sid#81689bc][rid#817a8c4/initial/redir#1] (1) redirect to http://www.domain2.com/landing_page.php [REDIRECT/301]

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22698285
RewriteCond %{REQUEST_URI} !^[/]*(landing_page\.php)?$
RewriteRule ^(.*)landing_page\.php$ / [R=301,L]
RewriteCond %{REQUEST_URI} .*
RewriteRule ^(.*) http://www.domain2.com/$1 [R=301,L]
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:LongShotBuddy
ID: 22698382
Thanks again ahoffmann!

Now, www.domain1.com/ is being redirected to www.domain2.com/, please see attached masked rewrite log.

Again, the behavior I'm looking for:

1) if www.domain1.com/ is requested, no redirect should occur, landing_page.php (the DirectoryIndex page) should be served
2) if www.domain1.com/page1 is requested, it needs to be redirected to www.domain2.com/page1
3) if www.domain1.com/landing_page.php is requested, it needs to be redirected to www.domain1.com/

I think the "root" of the problem is that when / is requested, apache serves up landing_page.php (it's the DirectoryIndex). This is not a redirect, it's returning the page defined as the DirectoryIndex. This action (not a user request if I understand the inner workings correctly), is passing the condition for the domain2 forward, where I don't want that rule to be executed in that case.
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (2) init rewrite engine with requested uri /
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (3) applying pattern '^(.*)landing_page\.php$' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (3) applying pattern '^(.*)' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (4) RewriteCond: input='/' pattern='!^/$' => not-matched
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (3) applying pattern '^(.*)landing_page\.php$' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (3) applying pattern '^(.*)' to uri '/'
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (4) RewriteCond: input='/' pattern='.*' => matched
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (2) rewrite / -> http://www.domain2.com/
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (2) explicitly forcing redirect with http://www.domain2.com/
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (1) escaping http://www.domain2.com/ for redirect
123.234.345.456 - - [12/Oct/2008:11:40:27 -0600] [www.domain1.com/sid#816c9dc][rid#8179ab4/initial] (1) redirect to http://www.domain2.com/ [REDIRECT/301]

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22698458
> .. (4) RewriteCond: input='/' pattern='!^/$' => not-matched

my last suggestion did not contain such a condition
0
 

Author Comment

by:LongShotBuddy
ID: 22698494
I'm sorry hoffmann, it appears I didn't comment out part of the prior attempt, corrected, and www.domain1.com/ still is being forwarded to www.domain2.com/ as attached.

Thanks so much for your prompt responses :)
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (2) init rewrite engine with requested uri /
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (3) applying pattern '^(.*)landing_page\.php$' to uri '/'
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (3) applying pattern '^(.*)' to uri '/'
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (4) RewriteCond: input='/' pattern='.*' => matched
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (2) rewrite / -> http://www.domain2.com/
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (2) explicitly forcing redirect with http://www.domain2.com/
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (1) escaping http://www.domain2.com/ for redirect
123.234.345.456 - - [12/Oct/2008:12:25:09 -0600] [www.domain1.com/sid#8134f24][rid#81794ac/initial] (1) redirect to http://www.domain2.com/ [REDIRECT/301]

Open in new window

0
 

Author Comment

by:LongShotBuddy
ID: 22698751
I kept messing with it, with the examples you gave, and I'm 99.9% there now, I just have one remaining question, and it's solely my lack of regular expression understanding I think:

While testing, I found that if I requested www.domain1.com/landing_page rather than www.domain1.com/landing_page.php, the forward isn't happening.

What do I need to change in:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*landing_page\.php\ HTTP/

to make it apply to both landing_page -and- landing_page.php?  And likewise what needs to be changed in:

    RewriteCond %{REQUEST_URI} \.(php|htm|html)$  

to make it apply to .php, .htm, .html, and -no extension-?

I've never had to work with regular expressions, I guess I need to do some studying..

Thanks for all your help. Attached is the -almost- final solution:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*landing_page\.php\ HTTP/
    RewriteRule ^(.*)landing_page\.php$ / [R=301,L]
 
    RewriteCond %{REQUEST_URI} !^/$
    RewriteCond %{REQUEST_URI} \.(php|htm|html)$   
    RewriteCond %{REQUEST_URI} !^/landing_page\.php
    RewriteRule ^(.*) http://www.domain2\.com$1 [R=301,L]

Open in new window

0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 22700650
> .. to make it apply to .php, .htm, .html, and -no extension-?
RewriteCond %{REQUEST_URI} (\.php|html?)?$
0
 

Author Comment

by:LongShotBuddy
ID: 22704483
Thank you!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL rewriting - LAMP 7 51
slow web access in oversea location 3 72
How can I install php on a ngxin server on droplet 5 159
Create link to folder for use with apache. 7 82
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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