I am really a noob when it comes to PHP and I would like to ask for your help. I would like to create a 404.php for my Wordpress site that shows the 404 page in the appropriate language. The site is multilingual: Dutch and English. (I tried some plugins but they don't work with my current theme). I have done some research and found some bits and pieces and mastered a piece of php code that doesn't work. If you could be so kind to point me in the right direction, that would be great. This is what I came up with:
<?php
$incomingUrl = $_SERVER['REQUEST_URI'];
if($incomingUrl == 'damcms.com/en') {
wp_redirect(damcms.com/en/page-not-found-404);
exit;
} else {
wp_redirect(damcms.com/pagina-niet-gevonden-404);
exit;
}
?>
I don't get an error message, nothing is being shown. So I miss something, what?Thanks in advance.
Claudia
First, the reason nothing happens is here:
wp_redirect(damcms.com/en/page-not-found-404);
Any time you have a string in PHP it must be in quotes; otherwise PHP tries to interpret it as instructions (and damcms.com/en/page-not-found-404 is not a valid PHP instruction).
So this would work:
wp_redirect('damcms.com/en/page-not-found-404');
But there's a bigger issue: you don't want to just check if the URL is exactly damcms.com/en — you want to check if it starts with damcms.com/en.
So, perhaps this:
if(strpos($_SERVER['REQUEST_URI'], 'damcms.com/en') !== false)) {
wp_redirect('damcms.com/en/page-not-found-404');
} else {
wp_redirect('damcms.com/pagina-niet-gevonden-404');
}
exit;
But going one level deeper, even this is a little iffy. WordPress already has a 404.php template that shows for any 404 page. I strongly recommend editing that — and there do the if ... 'damcms.com/en' test and then perhaps just include a 404-en.php template. That way the existing 404 mechanism still works. With the setup you have, the visitor is being redirected to a 200 page, so to a search engine it will appear your site has no 404s at all and everything is valid content!
Related
I am trying to execute code with PHP but only IF the URL is EXACTLY at the entry point of the website: http://mywebsite.com. So specifically ONLY on that URL, nothing after.
I am stumped after trying multiple PHP IF ELSE statements to try gaining it, very close I feel.
<?php $host = $_SERVER['HTTP_HOST']; if($host == "www.mywebsite.com" or $host == "mywebsite.com") { ?> MYHTML1SHOWS <?php } else { ?> MYHTML2SHOWS <?php } ?>
This has given me success in appearing on the domain when most visitors will come to mywebsite.com, but continues to work on all subsequent sub files/pages/directories. Which is 100% not wanted.
So I thought of a work around like ELSEIF's to show MYHTML2 to target all my pages, as they are handily all within country allocated directories: /au/ , /asia/ , /nz/ , /uk/ etc.
<?php } elseif (stripos($_SERVER['REQUEST_URI'],'/au/') !== false) { ?> HTML3 <?php } ?>
This didn’t work, but it was worth a try (works on its own IF statement in previous websites I’ve done, but I figure its clashing with the first IF statement which is more prioritized in the PHP). Appreciate any help guys, this has me stumped but would be ever useful. There were no similar questions on the net for only showing code this way.
(Background: I am implementing a 'Country Selector' that shows only on the entry point of mywebsite.com. I have already set up each country within their own sub-directories, thus no purpose of showing the country selector for them if a customer goes directly to one of those addresses).
You're probably doing some magic with Apache's mod_rewrite ... if not, that might be a good place to start looking. It sounds like the problem you're trying to solve is best done via Apache, either in your httpd.conf or (if enabled) via .htaccess.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Otherwise, the $_SERVER variables $_SERVER['PHP_SELF'] and $_SERVER['REQUEST_URI'] are probably of use to you.
http://www.php.net/manual/en/reserved.variables.server.php
$_SERVER['HTTP_HOST'] will only check the domain name, not the requested path. This is as you describe, but doesn't seem to be what you want.
So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.
I'm owner of some web site with dozen of web pages. Pages were made by using PHP. Before some time I discovered that some guys by using Joomla CMS and wrapper menu option included starting (login page) there and on this way confused members and other visitors, especially because "window" of wrapper isn't enough big and some information on my page aren't visible. On this way visitors connect these pages with me and get bad feeling about whole my site. I contacted these guys but no answer, then I tried to solve it by using $_SERVER['HTTP_REFERER'] super variable but I didn't get right and working solution for this problem. Someone experienced similar problem? Thanks.
EDIT - This is the code
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
if ($HTTP_REFERRER) {
// check if the referrer is on your noentry list
// if so redirect it to another page
if ($HTTP_REFERRER == "www.mean.visitor.com") {
echo 'referer is' . $HTTP_REFERRER;
die;
} // shows the referrer and formats ur local harddrive echo "You came from $HTTP_REFERRER";
} else {
//everything is OK
}
from the code you posted the first problem i see it's on the first line:
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
should be
$HTTP_REFERRER=$_SERVER['HTTP_REFERER'];
Then in the second if you must insert the web addresses you want to block. so change
if ($HTTP_REFERRER == "www.mean.visitor.com")
with
if ($HTTP_REFERRER == "the address yo want to block")
And write die() instead of die.
has something changed?
Hi
I’m trying to use the following cookie to force users to go to my homepage first no matter what link they come to me from (I know a lot of people frown upon this but it’s something i wish to do) if they haven't been to my homepage before:
I have the following code on my home page between the “head” tags:
$.cookie(“seenhp”, true, { expires: 365 });
And the following code on every other page between the “head” tags again:
if (!$.cookie(“seenhp”)) location.assign(“/index.php”);
It doesn’t seem to be working am i missing something very obvious?
you tagged this with PHP, but I cant see any PHP code here. This looks more like jquery?
Have you included jquery?
Well the PHP way would be
setcookie("seenhp", "seenhp");
and
if(!isset($_COOKIE["seenhp"])){
header("Location: http://home.page/");
die();
}
Not meaning to be offinsive, but please try to understand what you're doing.
Reading the other thread, i guess you don't use:
https://github.com/carhartl/jquery-cookie
Typical scenario:
DB items are displaied in page http://...?item_id=467
User one day deletes
the item
Google or a user
attempts to access http://...?item_id=467
PHP diggs into DB and sees items does not exist anymore, so now PHP must tell
Google/user that item is not existing via a 404 header and page.
According to this answer I undertstood there is no way to redirect to 404 Apache page via PHP unless sending in code the 404 header + reading and sending down to client all the contents of your default 404 page.
The probelm: I already have an Apache parsed custom 404.shtml page, so obvioulsy I would like to simply use that page.
But if i read an shtml page via PHP it won't be parsed by Apache anymore.
So what do you suggest me to do?
Is there maybe some trick I could use palying with htaccess too?
Thanks,
Hmm. Two ideas come to mind:
Redirect to the 404 page using header("Location:...") - this is not standards-compliant behaviour though. I would use that only as a last straw
Fetch and output the Apache-parsed SHTML file using file_get_contents("http://mydomain.com/404.shtml"); - also not really optimal because a request is made to the web server but, I think, acceptable in most cases.
I doubt there is anything you can do in .htaccess because the PHP script runs after any rewrite rules have already been parsed.
IF you are using apache mod_php, use virtual('/404.shtml'); to display the parsed shtml page to your user.
I was trying to do this exact same thing yesterday.
Does Pekka's file_get_contents/include result in a 404 status header being sent? Perhaps you need to do this before including the custom error page?
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
You can test using this Firefox extension.
I was looking exactly for something like you needed, so you have a page:
http://example.com/page?item_id=456
and if later you want that if item is missing you are redirected to:
http://example.com/page_not_found?item_id=456
In reality I found it is much more maintainable solution to just use the original page as 404 page.
<?php
$item = findItem( $_GET['item_id']);
if($item === false){
//show 404 page sending correct header and then include 404 message
header( $_ENV['SERVER_PROTOCOL'].' 404 Not Found', true );
// you can still use $_GET['item_id'] to customize error message
// "maybe you were looking for XXX item"
include('somepath/missingpage.php');
return;
}
//continue as usual with normal page
?>
So if item is no longer in the DB, the 404 page is showed but you can provide custom items in replace or error messages.