Solved

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

Posted on 2008-10-10
9
225 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
 

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IPv6 Networking 6 95
How can I install php on a ngxin server on droplet 5 121
transferring settings for apache and mysql 2 83
PHP Error Settings 12 78
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…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

12 Experts available now in Live!

Get 1:1 Help Now