When people visit my main page, index.php, I want them to be redirected to index.php?page_id=2
To do this, I added a simple html code in the head:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.domain.com/index.php?page_id=2">
The problem is that the destination and all other pages of my site runs that code it goes on a loop. I am using Wordpress. Is there any solution to direct them to index.php?page_id=2 upon visiting index.php without it going on a loop?
Any assistance would be greatly appreciated.
To avoid infinite loops, just check if the $_GET variable is empty. If so, do a redirect:
if (empty($_GET))
{
header('Location: index.php?page_id=2');
}
Fundamentally you need to make sure that your "meta refresh" is not inserted when index.php has arguments (such as page_id) on its query string.
If you were just doing this in PHP, you would write an if statement inspecting the contents of the querystring using the $_GET hashtable, i.e. if $_GET["page_id"] ...
http://www.php.net/manual/en/reserved.variables.get.php
In wordpress (which I know less about), you may find a different solution altogether is what you want. For instance, a meta-refresh is actually considered inferior to what's called an HTTP redirect - a header which has the same effect.
In PHP, it is:
http://php.net/manual/en/function.http-redirect.php
I notice that the wordpress documentation contains references to redirect utilities; I tried to provide the links, but StackOverflow blocked me from including any more. A search for "redirect" within their documentation may be helpful.
In general, I would expect to see an if statement that decides whether or not to issue a redirect, either using PHP's mechanism, or Wordpress'.
I hope this is helpful.
Related
can I use point in time?
something like:
header('refresh:1.5; url=xxx');
Yes you can use:
header('refresh: 2; url=someurl.php');
Where 3 is the time in seconds. More info about the header can always be found here.
But anyway I always advocate a combination to avoid problems with some browsers:
Header (as you're doing)
Meta tag (in the HTML head)
JavaScript timeout
When using the header I don't think you can use a decimal like 1.5. In any case I doubt any user notices the difference between 2 and 1.5 seconds. But if this is important to you, you can achieve it by using the JavaScript timeout.
Redirection ways
Meta tag:
<meta http-equiv="refresh" content="2;url=http://www.yourwebsite.com/someurl.php">
JavaScript:
setTimeout(function() {
window.location = "http://www.yourwebsite.com/someurl.php";
}, 1500);
header( "refresh:5;url=wherever.php" );
ref: http://php.net/manual/en/function.header.php
Yes u can, but before any headers already sent:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
http://php.net/manual/en/function.header.php
If you need time between 1 and 2 second you can use 1.5 as u declared in your question.
refresh is not part of the HTTP standard. It is a legacy method introduced by Netscape and implemented by other browsers as well. Even if it works on one browser you cannot be certain it will work on all browsers.
The W3C discourages the use of it, for details see this Wikipedia article. Especially the sections "Drawbacks" and "Alternatives" are of interest.
I'd prefer to use this over header('refresh ...'):
usleep(1500000);
header("Location: xxx");
This will send a clean 302 redirect and make sure it's HTTP conform and works with every browser.
I've heard that the usual way I redirect from an HTML page, like
<meta http-equiv="REFRESH" content="0;url=page.html">
is deprecated by the latest HTML. Is it true or not, and if so, what other ways are there to redirect?
The proper way to redirect is to send redirect headers.
You need to change status from 200 OK to appropriate 3xx status. Then you also need to include Location: http://yourRedirectURL header. The implementation depends on what programming language you are using in the back-end.
Using the Location header is both seamless and a more efficient way to redirect someone to another page, assuming you're just using a zero timeout anyways.
Unless you're placing them on a landing page first then redirecting them, use the Location header.
I should also note that the location header specifies it should be provided with a fully qualified address to land on and not use an absolute or relative site-based path. E.g.
Location: http://www.google.com/
Instead of:
Location: /login
Location: ../../home
If you are using php, you can use the following code (prior to any other output to the browser):
<?php header('Location: http://example.com'); ?>
It is technically not deprecated, but that’s just because the pseudo-term “deprecated” is sloppily used in the “spec”. The meta redirect mechanism is described as “should not” in HTML 4.01:
“Note. Some user agents support the use of META to refresh the current page after a specified number of seconds, with the option of replacing it by a different URI. Authors should not use this technique to forward users to different pages, as this makes the page inaccessible to some users. Instead, automatic page forwarding should be done using server-side redirects.”
The HTML5 drafts, though, describe the meta refresh mechanism without saying such things, though the examples are about different use. This does not make it any better idea. It should not be used for redirecting an address to a new one, except in case you have no way of affecting server behavior so that appropriate HTTP redirect takes place. In that case, it is advisable to add a normal link to the new address into the document body, for situations where the meta redirect does not work.
Is there some secret and mystical way to change the value of my HTTP-request's referer, or at the very least, keep it from showing? Also, using a MitM page from another domain would not solve my issue, as you are now just submitting that other page's value.
This is not browser specific, I would need to do this on the HTML level.
The problem I am facing is a silent-login page where it sends an HTTP-Redirect to the http-Referrer, unless it is the same domain, or empty.
You can not control this on an html level. Your only option is to modify the login code to not issue the redirect or to direct it to the desired page.
It's an old question, but I know how you can do this. The first way is not guaranteed across all browsers, but you can use rel=noreferrer. AFAIK GC is the only UA to currently support this but it is in the standard. FX may also, IDK.
The second way is far more reliable, and it involves a cool little hack someone shared with me on IRC:
Basically, construct an iframe from a base64-encoded data: URI. The framed document is to have a script that listens for a window.postMessage() and when it gets fed the command with a URL to visit, it executes window.top.location = msg.data.URI or however it is that one reads the message. Sorry I can't recall, I haven't slept for a few days.
Enjoy if you still care.. :)
I need to implement a forwarding. I did it the following way:
<html>
<head>
<meta http-equiv="refresh" content="0; URL=http://www.xyz.com">
</head>
<body>
</body>
</html>
Are there any situations in which this won't work? I read on selfhtml.org (http://de.selfhtml.org/html/kopfdaten/meta.htm#weiterleitung, sry for the german link couldn't find another) that this isn't always appropriate. Are there any better ways to do this? And in which situations my code wouldn't work?
Well, the major argument against is what the linked page already says: The user's browser could have meta redirects turned off (although this is going to be rare), and immediate redirects can cause usability problems when the user tries to navigate through the history.
If you can, though, don't output any HTML at all, but do a server-side redirect using the location header. In PHP, it would look like this:
<? header("Location: http://www.xyz.com");
die();
?>
if you can't do that, I'd say using a meta redirect is fine. You could add a few seconds' pause and a message ("You are now being redirected to...."), combined with a link, to minimize annoyance for users.
As for Search engine optimization, Search engines, I expect, will silently ignore the redirecting page, and go on indexing the target site, which is probably what one wants.
the site addres: http://www.ynet.co.il/YediothPortal/Ext/TalkBack/CdaTalkBack/1,2497,L-3650194-0-68-544-0--,00.html
fill the form with rubbish.
Hit 'Send'
the form post the data to another HTML without any parsing of the data i've just added
How do they do it?
A likely option is that they are using a content management system where "html" on the URL doesn't actually mean it's a static html file.
This may be out of left field, but I've certainly used the occasional JS function to grab everything in the header and either parse it or pass it to another script using AJAX.
I'll sometimes use this method in a 404.html page to grab the headers of the previous page, parse them out to see where someone was trying to go and redirect them.
That is, as annakata said, one of the numerous options available.
Edit based on clarified question:
Numerous frameworks can be configured to intercept an html request - for instance asp.net can be set to handle any given extension and an HTTPModule could do anything with that. It's really up to web server configuration what it decides to do with any request.
also: you don't really want to be saying "hijack"