PHP $_SERVER('REQUEST_URI') Showing Unexpected Result (httpd.conf, ssl) - php

NOTE: I am not the system admin, so my knowledge and ability to edit system config files is somewhat limited.
A server that recently went all-Https was not displaying our 404 page like it was before the switch.
Not Found
The requested URL /toh was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
In our httpd.conf page, we had the line
ErrorDocument 404 /admin/404/
When we changed this line to
ErrorDocument 404 https://example.com/admin/404/
everything seemed to work fine; the Not Found error went away and the 404 page was properly displayed.
However,
This 404 page detects where the user was originally coming from using
$_SERVER['REQUEST_URI'];
The page would then check a database, see if that URI was in there, and would then redirect the client to the appropriate page.
So, for example, if the user came to the page from:
http://example.com/tacos
In the event that wasn't a valid link, the 404 page would see the /tacos link, find it in a database, and redirect the user where he/she needed to go.
Now, the $_SERVER['REQUEST_URI'] only spits out
/admin/404
regardless of where the user was coming from.

So apparently the problem was twofold:
1) Per this thread here, the ErrorDocument link must not have the domain name; it must be in the format
ErrorDocument 404 /admin/404
2) In the .conf file, the did not have the RewriteEngine turned on; it was commented out. So we uncommented the following rules
<VirtualHost 127.0.0.1:80>
...
# RewriteEngine on
# RewriteCond %{HTTP_HOST} ^example\.com [NC]
# RewriteCond %{REQUEST_URI} /$ [NC]
# RewriteRule ^(.*)/$ https://www.example.com/$1 [L,R=301]
...
RedirectMatch permanent ^(.*)/$ https://www.example.com/$1
</VirtualHost>
Now, the 404 page is found and $_SERVER(['REQUEST_URI']) has the appropriate resource.

Related

htaccess not redirecting to custom 404 page

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]

Get the site root host name dynamically in .htaccess

I am trying to make 404, 500 etc pages in ErrorDocument in php .htaccess file, it works fine if give
ErrorDocument 404 http://localhost/project/errordocs/404.html
but i do not want to hard code the url here, instead i want to get the root url site name dynamically so that i do not have change it again and again as the hostname changes.
Basically i want to get this root url like: http://localhost/project can change to http://www.example1.com/project or http://www.example2.com/project etc. This url must come from projects root folder.
So that will dynamically become:
ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html
Any help please?
The asker asks incorrect. All what he writes
ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html
may be done by
ErrorDocument 404 /project/errordocs/404.html
But really he want: while moving site from project folder to project1, he should not change the rule
I think it may be done by htacces placed in /project with code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ errordocs/404.html [L]
It will work if AllowOverride set to All (it is default).
The problem only that responce in this case will be 200 but not 404
Based on the conversation you had, you would like to not have to change the path to the error document when you move your project to another folder with a different name.
Firstly, you cannot use variables when using ErrorDocument. The path that you provide must be static. The path that you specify must be either to an external URL (in which case your browser will be redirected) or to a file relative to your document root (i.e. localhost).
Unfortunately, ErrorDocument won't be able to find the file relative to the current directory (i.e. project). The only logical way of doing this would be to remove the leading slash, but this would cause Apache to render that as a string in the browser.
This brings us to the only other possible solution: mod_rewrite. The only problem with using it, however, is that it is processed early on in the mapping pipeline which may allow other modules (such as mod_proxy) to affect the process.
That said, you can try with the following:
/project/.htaccess:
RewriteEngine on
# Determine if the request does not match an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If so, send the request to the applicable file, relative to this directory
RewriteRule ^ errordocs/404.php [L]
# Per your comment and suggested edit, add the following.
# Note: This should not make any difference as mod_rewrite and PHP should
# already handle the error document.
ErrorDocument 404 /errordocs/404.php
/project/errordocs/404.php:
This file will send the 404 header as .htaccess won't be able to do that.
<?php header("HTTP/1.0 404 Not Found"); ?>
<h1>Sorry, we couldn't find that...</h1>
<p>The thing you've requested doesn't exist here. Perhaps it flew away?</p>

404 Page wont show / no .htaccess Wordpress

I want to show my 404 error page when ever someone gets on a non working / non created page. After a long time of searching on the web I couldnt find any solution so I post one myself. I dont have any .htaccess file either i created it filled it in but in my FTP it kept saying nothing was safed in there and it didnt show any bytes so thats something im also worried of.
So how can i get my .htaccess file to work and show my 404 Error page template? I followed all the steps to make a 404.php file but i dont mind it to be redirected to the same template I made for it but then as a page any of those solutions is fine.
You've got some rewrite rules in your .htaccess file that are preventing a 404 error from every truly happening...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
That basically states that if the requested resource is neither a file nor a directory (basically a 404 error), redirect to /index.php
Try this in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# delete the line below to have /index.php/anything trigger a 404
RewriteRule ^index\.php$ - [L]
</IfModule>
# END WordPress
#404 redirect
ErrorDocument 404 /httpdocs/wp-content/themes/avian/404.php
Note
This may make Wordpress quite unhappy however, I'm not sure how it uses internal routing, it may rely on breaking 404s to determine which pages to load by using fake (RESTful) URLs - reinstating 404 errors make cause Wordpress to stop working properly ... I am, however, no expert on it having barely ever touched it.

i want to output on 404 page the url the user was trying to use that resulted in the 404 page

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.

htaccess ErrorDocument 404 redirect not working

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.

Categories