Solved

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

Posted on 2008-10-10
9
224 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
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:LongShotBuddy
Comment Utility
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 51

Expert Comment

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

my last suggestion did not contain such a condition
0
 

Author Comment

by:LongShotBuddy
Comment Utility
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
Comment Utility
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
Comment Utility
> .. to make it apply to .php, .htm, .html, and -no extension-?
RewriteCond %{REQUEST_URI} (\.php|html?)?$
0
 

Author Comment

by:LongShotBuddy
Comment Utility
Thank you!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now