I posted this earlier
301 Redirect of Static HTML to Dynamic PHP Page
But have a new idea, and am wondering if there are any issues why I should NOT do this...
If someone tries to go to a dead page on our site like:
(domain)/somepage.html
That now exists here:
(domain)/dynamic.php?id=1
It fails and goes to a custom Error 404 page (/404.php)
If I look at the $_SERVER['REDIRECT_URL'] variable, I can see where they were trying to go. My idea is to add an include at the top of the 404.php page to check this value, and if it's in my list of items to redirect, then to use PHP to do the 301.
Something like this...
// -- php include at top of 404.php page
switch(trim($_SERVER['REDIRECT_URL'])){
case "/oldpage.html" : $location = "/dynamic.php?id=1"; break;
case "/oldpage2.html" : $location = "/dynamic.php?id=2"; break;
}
if(isset($location) && trim($location) != ''){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
exit(0);
}
// -- end of php include
This gives me a single point to enter in all the links I see in the google webmaster tools that are in blog entries, etc. that are now dead.
Thanks
Well, yes. 301, accompanied by a Location header, is the proper response for a request that you can positively identify as being moved.
Related
I havea code that should redirect in case it doesnt have some request parameter set correctly.
if(!is_numeric($_GET['id'])){
header("HTTP/1.0 404 Not Found");
header('Location: '.$url);
exit();
}
Problem is that whenever I check with Firefoxes plugin, live HTTP headers, I see 302 temporary redirect. why is that? why no 404 response is given?
It doesn't make sense to send a Location header with a 404 status code.
Location means "What you asked for is over here"
404 means "I don't have what you asked for"
The two statements are incompatible.
If you want to send a particular human readable explanation of the error, then just output it as you would for any other kind of document. You could include() it if you like. Don't try to redirect to it.
Problem is that whenever I check with Firefoxes plugin, live HTTP
headers, I see 302 temporary redirect. why is that? why no 404
response is given?
Not sure what you want to do here but following will provide your purpose.
if (! is_numeric($_GET['id'])) {
header('Location: ' . $url, true, 404);
exit();
}
You can only send ONE of the headers you are attempting as stated above.
In case of 404 - this will display the error relevant error pages as defined by you're web server config.
To do what you want... you will need to modify the server's 404 from a plain html to a php page and work out what to display from there (you can base on referrer etc).
From within the 'dynamic' 404 page, you can then do you're redirect if required.
I have a site I'm working on with a /landing-page/ folder, and I'll be making a number of landing pages. I'd like to be able to put urls that have /lp/ instead of /landing-page/ in advertisements, and have any url with /lp/ in it replaced with /landing-page/.
Thus:
www.site.com/lp/ipad-inspections
would automatically redirect to:
www.site.com/landing-page/ipad-inspections
I would like to do this without having a /lp/ folder and a second page corresponding to each landing page. My thought is to have the 404 page check the url and redirect, but I can't seem to get the following to work:
<?php /* Automatic redirect for landing pages */
$current_loc = $_SERVER['REQUEST_URI'];
$short_lp = '/lp/';
$long_lp = '/landing-page/';
if (strpos($current_loc, $short_lp)) {
$current_loc = str_replace($short_lp, $long_lp, $current_loc, 1);
header("location: ".$current_loc);
}
What am I doing wrong here, that my page is coming up blank? I have narrowed it down to the first line in my if statement, which is crashing my page.
If there a better way to do this with apache? Some line I can put in htaccess, maybe?
RedirectPermanent /lp /landing-page
in your .htaccess should do the trick. Best to do this sort of unconditional redirect BEFORE it reaches the PHP stage. It's essentially a "free" operation in Apache, and saves your server the whole parse/compile/execute PHP stages.
Keep it as you've done it but include the 301 permanently moved header.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Also try referencing your URL absolutely, so as to include http://www.domain.com/
Update
If you want to redirect with htaccess try
redirect 301 /lp/landing-page/ http://www.domain.com/new.html
I have a dynamic review system in place that displays 30 reviews per page, and upon reaching 30 reviews it is paginated. So I have pages such as
/reviews/city/Boston/
/reviews/city/Boston/Page/2/
/reviews/city/Boston/Page/3/
and so on and so forth
Unfortunately, Google seems to be indexing pages through what seems like inference - such as
/reviews/city/Boston/Page/65/
This page absolutely does not exist, and I would like to inform Google of that. Currently it displays a review page but with no reviews. I can't imagine this being very good for SEO. So, what I am trying to do if first check the # of results from my MySQL query, and if there are no results return a 404 and forward them to the home page or another page.
Currently, this is what I have.
if (!$validRevQuery) {
header("HTTP/1.0 404 Not Found");
header("Location: /index.php");
exit;
}
Am I on the right track?
You need to output the 404 status, and show a response body (= an error page) at the same time.
if (!$validRevQuery) {
http_response_code(404);
// output full HTML right here, like include '404.html'; or whatever
exit;
}
Note that you cannot use a redirect here. A redirect is a status code just as the 404 is. You can't have two status codes.
You cannot do both send a 404 status code and do a redirection (usually 3xx status code). You can only do one of them: Either send a 404 status code and an error document or respond with a redirection.
As Pekka suggests, the best option is to do a 404 status, and then put your 404 page code after that.
It is bad practice for SEO if you just 301 (redirect) the page because then the search engines will continue to visit the page in order to see if the redirect is still there.
I'm creating a PHP CMS and have some system pages like a 404 page, a maintenance page, and an unauthorized access page. When Page A isn't found, the CMS will redirect to the 404 page; if the user doesn't have access to Page B, it will redirect to the unauthorized access page, etc.
I'd like to use the proper status code in the header of each page, but I need clarification on how to handle the header/redirect. Do I put the 404 header on Page A and then redirect to the 404 page or do I put the 404 status on the 404 page itself? Also, if the latter is the correct answer, what kind of redirect should I use to get there, a 301 or a 302?
If a user arrives on page A and that page doesn't exist, then do not redirect : just send a 404 error code from page A -- and, to be nice for your user, an HTML content indicating that the page doesn't exist.
This way, the browser (and it's even more true for crawlers ! ) will know that the page that is not found is page A, and not anything else you'd have tried to redirect to.
Same for other kind of errors, btw : if a specific URL corresponds to an error, then, the error code should be sent from that URL.
Basically, something as simple as this should be enough :
if (page not found) {
header("404 Not Found");
echo "some nice message that says the page doesn't exist";
die;
}
(Well, you could output something nicer, of course ; but you get the idea ;-) )
I'm not sure if the redirecting is the best way for doing this. Id rather use some built in functionality that is included into the project.
If the data is not found, do not redirect the user to another page, just send him an error message, like Hey, this site does not exists! Try an other one and so.
And not at the end, you should build into the code, the code-part from the answer of Pascal Martin.
I would do this into a function, and call it from a bootstrap or something with a similar behavior.
function show_error($type="404", $header = true, $die = false)
{
if($header)
header("404 Not Found");
echo file_get_contents($type.'.php');
if($die) die; //
// and so on...
}
So I'm using a single entry point found in a previous question of mine: How to include config.php efficiently?
We all know what 404 errors is, so no explanation needed there. However, when using the single entry point index.php?page=pokemon, it doesn't go to the 404 page when trying to access a page that is non-existent. So how can I solve this problem which would make it more user friendly and direct the visitors to my 404 page so they won't see all the PHP errors?
You should output your own error page (you could just include() a .html file, for example), and set the HTTP status code using the header() function:
header("HTTP/1.0 404 Not Found");
Of course, you must set the header before including the error template.
I assume you do sort of an if / elseif or a switch on the variable $page to figure out which page to display, right?
If so, just add an ELSE resp. default: branch which will display a custom error note
to the user if an unexpected value is passed as $page.
HTH
If you’re actually using user187291’s code, alter it like this:
$page = "home";
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
$file = "$page.php";
if (preg_match('/\W/', $file) || !is_file($file)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
// print error message
exit;
} else {
include $file;
}
But since you’re using header, you need to make sure that no output has been sent to the client. Otherwise the HTTP header is already sent and you cannot modify it. You can use the output control functions to buffer any output and discard it in case of an error.