Displaying update page once - php

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?

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: 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.

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.

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.".

Related

Redirecting traffic from a certain URL to a certain page

Happy Friday!
I'm trying to create a redirect via .htaccess, basically when a user visits our old support system, it will send them to the IP of the new support system... Via HTACCESS I created the following:
RewriteCond %{HTTP_REFERER} oldsystem.domain.com [NC]
RewriteRule .* http://newsystem.domain.com/pages/wiki-has-moved/ [R,L]
However, this fails to point the user from the homepage of the new system.... Please could someone help me achieve this redirect?
Note - I followed this question, but the results aren't working with the redirect from .htaccess:
Allow access to a certain page on a site, only if they come from a link on another certain page
Note 2 -
The systems are on two separate servers, both with different IP's. However, they DO use the same domain oldsystem1.domain.com & newsystem1.domain.com.
Note 3-
We still want users to be able to access the old content (staff members) but non staff members will be redirected to the new system. This is why we have had to point the redirect to an IP.
This is how I managed to overcome the issue:
RewriteCond %{HTTP_HOST} ^wiki.volocommerce.com$
RewriteRule ^(.*)$ http://hub.volocommerce.com/pages/wiki-has-moved/ [R=301,L]
RewriteCond %{HTTP_HOST} ^wiki.esellerpro.com$
RewriteRule ^(.*)$ http://hub.volocommerce.com/pages/wiki-has-moved/ [R=301,L]
Hope this helps anyone else :)
Have you activated the rewrite engine? Your htaccess should look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.2.3.4 . // replace with allowed ip address
RewriteRule .* http://newsystem.domain.com/pages/wiki-has-moved/ [R,L]
If it should be a permanent redirect add this [L,R=301] instead of [R,L], so the browser will keep the redirect in cache and directly switch to the new site without getting redirected again.
If you want to allow multiple ip addresses you can add multiple conditions like:
RewriteCond %{REMOTE_HOST} !^1.2.3.4 [OR]
RewriteCond %{REMOTE_HOST} !^5.6.7.8

How do I force HTTPS on a some pages? (PHP)

I have a single PHP file which handles credit card processing. It starts as a form to input the credit card number, then becomes a "confirm" screen (showing the credit card number) then once more to process and display the results. All three loads will be done with PHP submits (I realize the confirm might be better as Javascript, but I didn't write it). It is the only file in the directory which handles credit cards, and therefore it is the only one which needs httpS connection.
I have tried forcing this with the $_SERVER array, looking up the protocol used to connect from the prefix of the SCRIPT_URI (or other entry), but none had the prefix.
Is there a simple way to do this...i want ssl on 5 pages homepage, login, register, contact page and if user visit other page then he should be on non ssl version
Sorry for the questions, but my searches thus far here haven't uncovered a working solution, and I'm afraid I don't know what the best practice is.
Use this code on php pages you want:
if($_SERVER["HTTPS"] != "on")
{
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
exit();
}
You should investigate $_SERVER['HTTPS']. This will have a non empty value if https is used and an empty value otherwise.
If you detect a non https connection you can redirect the user, e.g. using php header() method.
Another way to achieve this would be to use .htaccess configuration (if you're running on apache web server):
RewriteCond %{HTTPS} !=on
RewriteRule ^creditcard\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
Using https for the whole website is a really good option, too.
Suppose you want to redirect 4 specific pages to https,
page1.php
page2.php
page3.php
page4.php
then you would do something like this:
Create a .htaccess file in your root directory and add the following lines to it.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.yourdomain.com to yourdomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(page1\.php|page2\.php|page3\.php|page4\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

.htaccess Apache mod rewrite rule always giving a redirect loop

I am normally fine at writing apache redirects but this particular client's server has been giving me problems :c
I am trying to redirect all sub-pages to the main domain but I am always getting a infinite redirection loop.
Here's my .htaccess file - what am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]
RewriteCond %{REQUEST_URI} !"^/$"
RewriteRule ^.*$ "http\:\/\/example\.com\/" [R=301,L]
whoa redirect infinity! but say if I do something like this:
RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]
This will redirect http://example.com/test to http://example.com and bam, it works!
Any guidance would be appreciated. Thanks!
// EDIT
The issue is that we have a mailing list plugin which sends the user a confirmation email. After clicking the confirmation button in the confirm email the user is redirected back to our site with a long query string appended to http://example.com/. The query string is generated so it's never going to be the same.
Here's an example of one of the query strings:
http://example.com/?has_media=false&fan=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Ffan%2Fshow%2F63451204%3Fartist_id%3D12053%26auth%3D96419cab29f59a342cde9ca3c0c20b58&campaign=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Fartist%2F12053%2Fcampaign%2F10151864
// Edit 2
I tried #Jon Lin's example and the page loads strangely but it does load. Basically, the correct domain loads in some browsers but the assets (CSS, and images do not) and the networks inspector is showing these paths as permanently moved also :s
Try changing your last rule to:
RewriteRule !^(?:index\.(html?|php)|)$ http://example.com/ [R=301,L]
The reason is that after the directory index is mapped (to something like index.php), the rules get reprocessed again, and thus the extra redirect.
Note that the query string is still going to be at the end. That's because there is no way to match against the query string in a RewriteRule. You need to match against the %{QUERY_STRING} variable. In order to get rid of the query string, you need to add a ? to the end of http://example.com/.

.htaccess. redirecting urls that has a "go" tag

I'm having a pretty hard time on .htaccess URL rewrites in PHP. I want to put a specific condition to it but it has not worked out yet.
I WANT TO WRITE A CONDITION TO REDIRECT EVERYTHING THAT HAS A "GO" IN ITS URL
RewriteCond %{QUERY_STRING} ^.*&go=([^?&]*)\??([^&]*)&*.*$
Redirect /alice1.html http://www.google.com'
the failed condition that i tried
FOR AN EXAMPLE IF SOMEBODY TYPES
'localhost/alice1.html' IT SHOULD NOT BE REDIRECTED TO google.com
BUT IF IT IS
'localhost//alice1.html?go=www.something_something.com'
THEN IT SHOULD BE REDIRECTED.
You can start from this base rule.
RewriteCond %{QUERY_STRING} \bgo=.+
RewriteRule . http://google.com? [L]
Then refine the checking of the go variable.
edit: note that this is just a template rule matching your input. On a production server it would be better to either set it as an external redirection (using [R=301, L] instead of just [L], assuming you want it permanent) or switch to an appropriate error page (403, 404...)

htaccess redirect without user knowing

We are running multiple domains through the same code and we want to save their images in their respective folders. Here's what we are doing.
/images/www.domain1.com/logo.jpg
/images/www.domain2.com/logo.jpg
now, what I want to know is, is this possible in htaccess that we rewrite the urls without user suspecting anything. This is what I want that
<img src="/images/logo.jpg" />
should internally become through htaccess
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]
But my question is,
The above redirect continually loops
Can I achieve the img effect without user or admin suspecting anything?
Sincerely,
Khuram
Remove the R=301 to simply do a rewrite rather than a redirect:
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L]
The reason it's continually looping is that the 301 redirect causes a new request to be created for the url images/www.domain1.com/logo.jpg. That URL also matches your ^images/(.*)$ rule, so it is redirected again, ad infinitum.
If you really want to do a 301 redirect (I suspect you don't, but if you did), you could solve the infinite looping problem by adding some rewrite conditions to skip the redirect if the domain is already included:
RewriteCond {REQUEST_URI} !^images/www.domain1.com/(.*)$
RewriteCond {REQUEST_URI} !^images/www.domain2.com/(.*)$
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]
You definitely should not use R flag if you don't want to change URL in browser. However even without R flag your RewriteRule will loop infinitely and you will eventually get internal server error. Use RewriteRule like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^images/(.*)$ images/%{HTTP_HOST}/$1 [L,NC]
Which is using a special internal variable called {ENV:REDIRECT_STATUS} that is set to 200 once RewriteRule rule is applied successfully.

Categories