Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-10
9
Medium Priority
?
234 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

972 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