views:

41

answers:

3

I am in the middle of developing a new website with my existing site still up and running. What I want to do is redirect the user to an update page when they visit the existing site (regardless of what page they enter on) then redirect them from that update page to the original page they requested.

I think the mod_rewrite rule would look something like

RewriteEngine On
# redirect to landing page
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1  // my real IP would go here
RewriteCond %{REQUEST_URI} !/UpdatePage.html$ [NC] // stop infinite loops on itself
RewriteRule .* /UpdatePage.html [R=302,L]

So the above would achieve redirecting the user from any page to the update page. However, evidently the issue here is it is going to cause a continuous loop...

I imagine I need some form of flag here? I am not really too experienced with Mod_Rewrite is something like this possible? Or is this something that can be achieved outside of Mod_Rewrite with pure PHP?

A: 

In my opinion this is the best way to go about it:

real site: www.example.org old site: old.example.org new site: new.example.org

The 'new.' domain needs to be in place well beforehand. When you cut over the site you can just redirect everybody hitting the old site to new.example.org. For a while your users will have the 'incorrect' domain. After a few days you can redirect "new." to "www.".

Evert
+2  A: 

Edit: I had a chance to play with my htaccess file and I did figure out a solution that I have appended below. Long story short though, I wouldn't use it even though it seems to work.

Original:

You can try not redirecting if you have already redirected by adding the REDIRECT_STATUS check:

RewriteEngine On
# redirect to landing page
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1  // my real IP would go here

RewriteCond %{ENV:REDIRECT_STATUS} 200

RewriteCond %{REQUEST_URI} !/UpdatePage.html$ [NC] // stop infinite loops on itself
RewriteRule .* /UpdatePage.html [R=302,L]

From what I understand this would mean: redirect to the UpdatePage.html if the remote address is a certain IP address and it has not been redirected before.

I'm unable to check whether this will result in the desired behaviour, but I did get this from the answer on this SO question: http://stackoverflow.com/questions/499068/clean-url-redirect-loop.

Tested solution:

I played around with my WAMP setup and came up with this:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1  // my real IP would go here
RewriteCond %{REQUEST_URI} !/UpdatePage.html$ [NC] // stop infinite loops on itself
RewriteCond %{REQUEST_URI} ^(.+)\.html$ [NC] // Only handle html pages
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^(.+)\.html$ [NC] //don't redirect if the referer is a page. Should actually include the domain as well
RewriteRule .* /UpdatePage.html [R=302,L]

The first time to any html page in the site should redirect to the UpdatePage then the user would click a link on that page and will go to the original page. The referer environment variable is then set and you can check if it is valid and then not redirect.

This does not handle going to the root of the website. The user must be loading an html page directly.

I much prefer your solution (@james). Coding the logic into the top of the main gateway PHP page is simpler and far less error prone, but I figured I would try to find a way using htaccess :-). Please note that I would never use this particular htaccess method myself.

Stephen Perelson
I tried this but nothing seemed to happen?
James
+1 For a mod_rewrite specific answer
James
+1  A: 

Due to the way the current site is implemented (everything goes through index.php page) I was able to simply use some session variables in order to detect whether the user should be redirected to the update page or not e.g.

<?php

if (!isset($_SESSION['ShowUpdatePage']) || $_SESSION['ShowUpdatePage'] == "true")
{
    $_SESSION['ShowUpdatePage'] = "false";
    header("Location: UpdatePage.html");
}

?>

This also means that they will only ever get redirected once per session which is exactly what I was looking for.

James
This is a much better approach to solving this type of problem. But since your question wanted an htaccess solution, I figured I would edit my answer and add something more likely to solve the problem.
Stephen Perelson
@Stephen - I wasn't looking for only solutions using Mod_Rewrite if you look at my post I mention is it possible using *"pure php"* aswell. However, +1 to your answer as you did manage to do it via Mod_Rewrite!
James
You should flag your answer as the solution :-)
Stephen Perelson
@Stephen - Can't for another 12 hours! Think you need to wait something like 24 hours before you are allowed to accept your own answer as the correct one.
James