Use .htaccess get Referrer URL from a 404 page - .htaccess

I am using .htaccess to get the Referrer URL. For example, if someone clicks the link example.com/dogfood-post-1 and the post is not found, how can I get that URL example.com/dogfood-post-1 in my 404.php page?
ErrorDocument 404 /404.php

Not really a full answer as it depends on specific configuration, but this should help you.
Add var_export($_SERVER) to your 404.php and see which key contains the value you need.

If you just want the url on the domain.com/dogfood-post-1 you could use:
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
source: get full url stackoverflow
if you send them to another page you could set a session and put the url in the session and then display it on the 404 page.
Instead of redirecting users to a 404 page you should show them the 404 error page. In your .htaccess you can add:
ErrorDocument 404 /appName/404.php
Put your absolute path before the 404.php in order to show it properly.

Related

How to redirect 404 error without changing the URL in PHP when all pages are loaded with index.php

I have developed a project where all files are loaded with index.php like index.php?page=add-member in this way, so now when I am doing ind.php?page=add-member so it is showing me the 404 not found error which is quite obvious or the normal behaviour but what do I want is that I want to include my custom 404.php page but without changing the URL, so far I have used this but I am getting redirected to 404.php.
.htaccess
ErrorDocument 404 /404.php
my index.php
$page = $_GET['page'];
include 'pages.php';
include $view;
pages.php
if (!empty($page)) {
switch($page){
case "add-member":
$view = 'folders/add-member.php';
break;
}
}
So how can i do that.

if not found any page or url then redirect index.php

I want to redirect like http://localhost/project/g47sh to http://localhost/project/index.php
But when I am write on URL http://localhost/project/g47sh then 404 error is generated so I want to if any page or directory not found the redirect index.php and also pass not found URL path.

Show failure link in 404 error custom page

Is it possible to show the user the link or the file, which can not be found through the html code of a 404 error custompage?
I asked google, but could not found any informations.
Also I do not know php and would now wondering
if it would be possible only with htaccess and html?
If not, what could I do with php, while using the htaccess redirect?
There are any simple ways?
My htaccess:
ErrorDocument 404 /funktion.fehler.php
If you're using the error document:
ErrorDocument 404 /funktion.fehler.php
Then you can use a couple of php variables. Something like:
print('The URL ' . $_SERVER['REQUEST_URI'] . ' does not exist');
You can also use $_SERVER['REDIRECT_URL'].

Default Directory Index

I have a website :
http://www.exclusivetech.net/clients/fashion/
If you look at the url then i have not mention any page/file name to be executed, it will display index.php bydefault.. I want it to show page not found error against this and any sub directory if particular filename is not mentioned in any URL .....
Write this in your .htaccess
ErrorDocument 404 /path/to/yourerrorpage.html

404 errors being redirected to the homepage instead of custom 404 page

I have a site hosted on an Apache server. I have created some custom error pages and the following text at the top of my .htaccess file:
ErrorDocument 404 404.html
ErrorDocument 500 500.html
ErrorDocument 401 401.html
I have also tried,
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
Both the htaccess file and the custom pages are in the root directory of the server.
The problem is that when I enter a garbage url (where I would expect to see my custom 404 page) I'm simply being redirected to my index page.
Try if your server is properly set up to parse and process .htaccess files in the first place (i.e., check if AllowOverride + AccessFileName directives are correct). For example, write some stuff in that you know will work and look if it actually gets executed (like a ridiculous rewrite rule). Also, look up your httpd log files for errors.
If it does get executed properly, the problem might be that your server is setup not to allow all kinds of overrides with .htaccess files. Your syntax however, is basically correct.

Resources