FallbackResource works for some pages but not others - php

I am using FallbackResource in the .htaccess file:
FallbackResource page.php
This is the only line I have in this file. When I type
www.test.com/test/no-page
I get redirected to page.php indeed. So this work (where no-page in this example is a page that of course doesn't exist). And to double check, if I removed FallbackResource from the .htaccess file, then this case doesn't work (I get an Error 500).
Now when I do:
www.test.com/test/no-page/no-page-2
I get an Error 500?
Is there anything else I need to do, or check? What I need is to have any pages not found being redirected to page.php whatever the URL is.
EDIT
Also in this question URL rewriting with PHP, someone suggests that the syntax should be:
FallbackResource /page.php
Which is also what they say in the Apache documentation: http://httpd.apache.org/docs/current/mod/mod_dir.html#fallbackresource
In my case, it only works without the /. Could someone shed some light on this as well?

Related

server error using URI embedded images in a required file if index.php isn't in the url

Struggling. Help!
In index.php:
require "cachedPages/home.html";
If I visit: https://websiteaddress.org/index.php then it works fine.
If I visit: https://websiteaddress.org then I get an internal server error.
I guess it's a .htaccess thing. All I have in there is some cpanel php72 code and:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
It seems there's some difference between how index.php is called if you call it directly as opposed to .htaccess calling it for you?!?
Any ideas?
Thanks in advance.
If you don't have one of the following options in your .htaccess then Apache won't know which file to default back to:
DirectoryIndex index.php
FallBackResource /index.php
Also I'd recommend installing mod_rewrite as well. It's handy for other reasons.
Well, I've managed to sort the problem, but it's a pretty weird situation that no-one else will probably ever experience. Here goes...
If the user lands at:
https://websiteaddress.org rather than https://websiteaddress.org/index.php
and that page (php) requires another page,
which has images that are embedded as URIs rather than linked src files.
Then, the first URI causes a server error.
If I replace the
img src='data:image/jpeg;base64,/998a9g98ahg...etc'
with
img src='path/to/file.jpg'
on the first instance of a jpg then it all works fine.
All the later URIs are fine, it's just the first instance!
It all works now, with this workaround; and the situation is so unique and bizarre that I doubt this thread will be of use to anyone else. In fact it's so edge-case that I can't be bothered investigating it any further myself.

PHP able to access pages in root directory from a page with slash

We're having a problem with our PHP site: http://midlandssmilecentres.co.uk/
The problem is as follows, if you were to access, for example http://midlandssmilecentres.co.uk/feedback.php, and then put a trailing slash, it returns the index page of the root directory. From there, you can actually put any page, i.e. http://midlandssmilecentres.co.uk/feedback.php/feedback.php, and it will return that page. We need this behaviour to stop, or a feasible work around to stop these pages being accessible.
This is causing us lots of problems with duplicate content and pages that haven't been mobilised, due to the page trying to fetch the CSS from the wrong directory. We've tried removing all of the .htaccess files on the server, however this has not fixed the issue. I've done plenty of research on files with trailing slashes and how a file can be interpreted as a directory, but couldn't find anything that could help me with this.
With .htaccess, you can redirect all .php/... pages:
RewriteEngine on
RewriteRule ^(.+?\.php)/ $1 [NC,L,R=301]
You also can change the apache configuration with:
AcceptPathInfo Off
But in this case a 404 error will be returned
https://httpd.apache.org/docs/2.2/mod/core.html#acceptpathinfo
I think you need to set a 404 REDIRECT, so instead of returning that page, it redirects to a 404 page.

.htaccess directory redirect to PHP argument

I have looked around and found a few examples, but they all cause:
500 internal server error
...so here's what I want to do:
I have a file called view.php in my root directory, I want it so if someone types www.test.com/view/16 it actually displays www.test.com/view.php?p=16. Although I don't know how to do this without causing an error.
I would provide code, but it doesn't work and when it doesn't error, it simply redirects me to the same place, it is also a lot longer that other answers I've seen, so its not a layout I want to use. If you really want to see it, let me know and Ill post it.
I also don't want the URL to change in the browser, so sharing is easier... any ideas?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^view/(\d+)/?$ view.php?p=$1 [L,QSA,NC]

FallbackResource causes net::ERR_INCOMPLETE_CHUNKED_ENCODING

When I use a .htaccess that contains just this line:
FallbackResource index.php
I get this error on Chrome only:
net::ERR_INCOMPLETE_CHUNKED_ENCODING
The error appears only when access the root directory where the .htaccess is located.
IE, if .htacess is in www.mysite.org/admin then the error only appears when I type that exact URL in the nav bar.
If I go to www.mysite.org/admin/index.php or www.mysite.org/admin/some-user-friendly-url there is no error.
The error is really annoying as it delays the page load by several seconds and makes the page fail to execute some JavaScript that should happen on page load.
Like many others with this error, it only happens in Chrome. Solutions from other posts were of no help in my particular case.
Add DirectoryIndex directive to .htaccess file before FallbackResource. This fixed the problem for me.
DirectoryIndex index.php

I have the URL www.something.com/HereWeGo, I want to get HereWeGo without the 404 error

Consider the URL www.something.com/HereWeGo. When someone enters the url, the site shows a 404 error since the directory does not exists. I can parse the URL to get the query and HereWeGo, but cannot stop it from going to a 404 error.
How can I achieve it by PHP, if it is possible to avoid .htaccess now
Thanks
Jean
You will need to rewrite your URLs so /something directs to index.php?q=/something, the thing is your web server usually throws a 404 by default if the file / directory cannot be found and there are no RewriteRules associated with it, even if you can parse the URL from the error page and find the information you need.
Is there a valid reason why you can't use .htaccess? I would strongly suggest you use it if you can. Not least because at the moment a 404 will mean Google will not index your page.
This article will help you.
You just can't avoid "touching" .htaccess, at least for rewriting all requests to single file ( or defining default 404 file ). You are using Apache, not PHP for the server, right?
Take Kohana 3 Framework for example ( and most other frameworks ), all requests are rewritten to index.php file, which then handles the routes for the request.
RewriteRule .* index.php [L]
Code below will handle everything that responds with a 404 and redirect it.
ErrorDocument 404 /somefile.php
In the somefile.php you can then check if referer is 'local' ( $_SERVER['HTTP_REFERER'] ) and which query string it was about, responding with whatever you want.
You could use mod_rewrite for that specific folder, or you could use your own custom 404 page (read this article).
this is something that you need to configure on the webserver, in a .htaccess file. Take a look at the syntax for the redirect or rewrite. It isn't possible to do this in PHP alone as it is the webserver which processes the URL request.
IIRC, setting up custom 404 (and other web server errors) are part of your webserver environment.
You can't avoid a .htaccess redirect unless you change your apache (or other webservers) configuration to redirect your 404 errors to your own *.php file instead of the default error message.
You have to setup a php file, such as 404.php as your 404 error handler file. After doing this have code in 404.php that looks something like this:
if (stristr($_SERVER["REQUEST URI"],"HereWeGo")
{
//add handling code here
}
On your destination page simply call--
header('HTTP/1.0 200 OK');
--to override the default 404 status code.

Categories