Link to home
Start Free TrialLog in
Avatar of LongShotBuddy
LongShotBuddy

asked on

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

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

Avatar of ahoffmann
ahoffmann
Flag of Germany image

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]
Avatar of LongShotBuddy
LongShotBuddy

ASKER

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

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]
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

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

my last suggestion did not contain such a condition
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

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

ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!