Please read the all. I know it is long but I really need help.
What I am trying to do is including the requested file in the "main-container", then rewrite the requested url, if it does not exists, show up 404 error page with .htaccess like ErrorDocument 404 /404.php
So, I know there is a lot of article about that things, but no matter I do, I could'nt succeed on this. There is always a problem. For example;
I tried to use .htaccess like that
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /page_handler.php
so every requests comes to page_handler.php first, it passes the requested page variable with $page to index.php, and index.php uses it to fill the page.
$page = $_SERVER['REQUEST_URI'];
if(empty($page) OR $page == '/') {
$page= "main";
}
include "index.php";
The problem with this one is, I can't use ErrorDocument 404 /404.php because whatever page is requested, it calls page_handler.php ,so the pages gets filled, no 404 error.
The second approach I tried is using .htaccess like that
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
and in the index.php
global $pages;
$pages = #$_GET["page"];
$pages = explode("/", $pages);
and check $pages[0],[1] etc to calculate the page and include through it. With this one, you cant use
ErrorDocument 404 /404.php
so I tried to check if file exists, if not
header("HTTP/1.0 404 Not Found");
echo "<div class='content-wrapper'>";
include('404.php');
echo "</div>";
but it only simulates a 404 page, it does not send a real 404 error to bots and when I use it, you can call a page like www.example.com/articles/1/whateveryoutype like www.example.com/articles/1/1294ajgakshja1
it calls article with id 1 but what is that last part?
So as you see, I am a rookie and a LOST one. Don't know what to do. I tried a lot of thing, read a lot of webpage, tried much more things than these mentioned ones, but cannot solve my problem. I need someone to direct me to the right path. As I mentioned, I just want to fill the main-content area with the requested file, rewrite url to SEO friendly, and 404 the page if it does not exists. Thank you
I might have not understood your question properly.
I better use this:
.htaccess:
ErrorDocument 404 /404.php
404.php:
<?php
header("HTTP/2 404 Not Found"); // Or use HTTP/1.0 or HTTP 1.1
// Any other includes
echo "The requested URL ". parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH). " was not found on this server.";
// Any other includes
?>
While including, use the correct path from the / directory (if using linux)
/home/username/public_html/include.php
Related
I have a custom 404 page called 404page.php , and I have the htaccess file with the line that I found should work (this is the only line I have in the .htaccess file)
ErrorDocument 404 /404page.php
It was working fine yesterday, but today I was optimizing images and some other stuff on the website, and when I reuploaded it, the htaccess didn't redirect to 404page.php anymore, but to a page that has this written:
Not Found
The requested URL /asfsdtg was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
(I just typed asfsdtg to test if it was working).
The website is uploaded online and I'm managing it through cpanel (I'm still a beginner). I searched for some solutions, I tried adding another line to .htaccess, then it looked like this:
RewriteEngine on
ErrorDocument 404 /404page.php
I even tried putting the location of the 404page.php as a local link, and the internet link, and it still gave me the weird error page.
Does anyone have some idea whats happening? If you need more info that I didn't supply please tell me what more I can supply
Try out this:
RewriteEngine on
ErrorDocument 404 http://yoursitename.com/404page.php
And be sure that 404page.php exists on the root of your server and is trully called 404page.php not 404Page.php
Be carefull at the characters, this is key sensitive!
Try with like this:
RewriteEngine on
ErrorDocument 404 http://www.sitename.com/404.php
Make to handle any 404 errors.
ErrorDocument 404 /404page.php
If above setting does not works you have to write addition line of code as mentions below.
Which identifies for non existing files and directories and redirect to 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]
I have a web app, and I want to redirect users to the 404 page without them actually ever going to a 404 page. Is this possible?
I added this line of code to my .htaccess file:
ErrorDocument 404 /404.php
So when a user types in (a page that does not exist on my website):
shareit.me/jsdhjfkhoe
They are redirected to:
shareit.me/404.php
Is there a way to redirect to the 404 while the URL remains:
shareit.me/jsdhjfkhoe
Use this to pass all paths to 404.php if they do not exist and preserve the URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
Set this header in the 404.php file.
header("HTTP/1.0 404 Not Found");
This line:
ErrorDocument 404 /404.php
doesn't change the URL in the client browser since it does only internal rewrite to /404.php
I suspect you have some other rule in your .htaccess doing this full redirect. If you post your full .htaccess then I can investigate.
Actually this should do the trick:
ErrorDocument 404 /404.html
-> Try using an html element named 404.html in your root!
Have a look at this if you want to see a full implementation: https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess
Otherwise you could have a look at the documentation if anything is unclear:
http://httpd.apache.org/docs/current/mod/core.html#errordocument
If you want to make your own customized Error 404 page. Here's what you need to write down on your .htaccess.
----------- .htaccess ----------------
# 1 ---- Establish a custom 404 File not Found page ----
ErrorDocument 404 /filenotfound.php
# 2 ---- Prevent directory file listing in all of your folders ----
IndexIgnore *
----------- .htaccess ----------------
Where filenotfound.php is your own 404 customized page. Hope it helped.
I have read lots of threads about htaccess redirect, but none really match my situation, albeit quite simple, got me pulling my hair out.
for example I have a site with this structure:
root
-gallery.php
And I have already implement htaccess rule to hide .php extension.
How do I catch the following events:
mysite.com/nonexistingfolder would go to mysite.com/404.php
mysite.com/gallery/nonexistingfolder would still load mysite.com/gallery, so that I can catch the /nonexistingfolder as a $_GET inside mysite.com/gallery.php?
Append this after RewriteRule
ErrorDocument 404 /error_docs/404
Replace /error_docs/404 with the file that shows the error or does something.
For the regular 404 page, use an error document:
ErrorDocument 404 /404.php
That covers #1, non-existing folder will go there.
For #2 try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)$ /gallery.php?page=$1 [L]
I'm trying to create a custom 404 page to include this information:
Sorry, this page was not found
The page you was trying to view was:
Please check the URL is spelt correctly, if it is then this page may
have been moved, removed or never existed
I have tried using: $_SERVER['HTTP_REFERER'] to capture the URL that the user was trying to view before they got automatically redirected to the 404 page
working .htaccess code
Options -MultiViews
RewriteEngine On
ErrorDocument 500 /500.php
ErrorDocument 404 /404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L,QSA]
It's working now, thanks everyone who helped me
You want to use $_SERVER['REQUEST_URI'] which gives you the URI which was given to access the current page. The result is in format /request_uri.php. Please refer to http://php.net/manual/en/reserved.variables.server.php for further details.
In your 404.php you could do the following:
The page you were trying to view was: <?php echo $_SERVER['REQUEST_URI']; ?>
Update:
Update your ErrorDocument declaration as follows:
# Remove http://www.example.com
ErrorDocument 404 /404.php
From the docs (http://httpd.apache.org/docs/2.2/mod/core.html#errordocument)
Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications, the most important being that the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code. In addition, if you use a remote URL in an ErrorDocument 401, the client will not know to prompt the user for a password since it will not receive the 401 status code. Therefore, if you use an ErrorDocument 401 directive then it must refer to a local document.
Please tell me why this is not working. It was working but has inexplicably decided to stop working.
In php I issue a 404 if the page is not found like so.
if(checkPageExists($escaped_url_page_name)){
header('HTTP/1.1: 200 OK');
}else{
header("HTTP/1.0 404 Not Found");
die;
}
My htaccess is
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
ErrorDocument 404 /404.php
The 404 page is on the root and is name 404.php. Why would that not work when five minutes ago it was and nothing has changed. I have tested the page with fiddler and it is indeed throwing the 404's but the redirect is not happening. I have friendly urls implemented, but I would not think this affects it as the "/" in the htaccess refers to the physical root of the site I thought. Thanks in advance.
I had a similar issue once - when PHP throws a 404 header, Apache generally won't make a 404 page for it. (Edit: It probably has to do with whether PHP outputs any text. I haven't really experimented with it much.) If you check the headers in your browser, you can see that the 404 header worked, even if the page is blank.
I'm not sure about an automated solution, but I just made an http_error($code) function that prints my custom error page and called that along with header() whenever my code manually triggered a 404. Then I bound a bit of code to ErrorDocument that calls http_error only:
ErrorDocument 404 index.php?http_error=404
ErrorDocument 403 index.php?http_error=403
...and so on.