Passing URL entered by user to 404 page with Htaccess and PHP - php

I have a website with PHP and htaccess support.
Basically, I want to pass the URL entered by the user (that caused the 404) to the 404 page itself.
Is there any way to do this?

Add following rule in your .htaccess file.
ErrorDocument 404 /error.php
In the error.php file.
<?php // error.php
echo $_SERVER["REQUEST_URI"]. " doesn't exists. Sorry!";
?>

Just add the following in your .htaccess file (where 404.php is your error page)
ErrorDocument 404 /404.php

You can do it by adding the following rule in .htaccess.
ErrorDocument 404 /404.php
In your 404.php you can write
<h3>
Page Not Found.
</h3>
<div>
<p>Apologies, but the page you requested could not be found.</p>
<ul>
<li>You may have mistyped the page address.</li>
<li>You may have clicked on a link that has not been updated.</li>
<li>We have recently changed our site structure and the page has been moved to new location.</li>
</ul>
</div>
You can style this to display it properly.
Hope this helps :)

Related

How to stop accessing the user from specific URL scheme

I was working on a Project & I added 404 Page not Found code in htaccess file of it.
This 404 Error Code working fine for all type of urls if someone accidentally type the wrong name of page etc.
But, I try to add the same name of page 2 times in the URL path and it show me something very strange.
Lets suppose i have a website http://example.com
It has a page named dashboard.php, So I first try to access it using http://example.com/dashboard.php. It show me the dashboard.php as normally it should be show me.
Issue:
When I try to access this dashboard.php using http://example.com/dashboard.php/dashboard.php, It show me all the texual content without including css,js,images files.
Screenshots:
http://example.com/dashboard.php
http://example.com/dashboard.php/dashboard.php
My htaccess code (Working fine):
ErrorDocument 404 http://example.com
In your error page, use only absolute links (with domain, or from root with / in the beginning), especially for css or images.
Or you can add in the header:
<base href="http://example.com/">
or
<base href="/">
You can stop the user from accessing url like this with htaccess :
AcceptPathInfo Off
https://httpd.apache.org/docs/2.4/en/mod/core.html

How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

I have a page (example.com) with anchors at major sections (example.com/index.php#anchor, example.com/index.php#about, example.com/index.php#contact, etc.) as well as subpages (example.com/subpage.php).
Here's my .htaccess file:
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php
RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]
The subpage rewrite rule works great.
For the anchor link, including the "R" (redirect) flag changes the URL to example.com/index.php#anchor, but I want the URL to be example.com/anchor.
If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor.
So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript?
It's not possible to do that with URL rewrite. Because the browser scroll down when the # is part of the URL... not on the server side.
You can't do this with .htaccess. But you can do it with the following JavaScript code:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

nofollow functionality for error404.php

My header.php contains header information for all my pages, but for the error404.php page needs the nofollow meta data meta name='robots' content='noindex, nofollow.
I tried this in header.php
<?php if($page_id = 'error404') { echo "<meta name='robots' content='noindex, nofollow'>"; } return false; ?>
/*error404.php*/
define("PAGE", "Error404"); $page_id ='error404';
But I can't seem to get it to work. This code currently kills all the css on the index.php file.
I'm using .htaccess for handing error 404s.
.htaccess
# Send user to error404.php
ErrorDocument 404 /error404.php
Any help would be appreciated.
Just call
header("HTTP/1.0 404 Not Found");
before any output (echo, print, ...). You can still have custom error page, but there will be no indexing.

Dynamic .htaccess 404

Currently, about half of our webpages derive from our HTML_page.php.
I want to add a <div class="Error404"> tag, 404 div, there to display 404 error information.
If a non-HTML_page.php webpages triggers a 404 error, I cannot display an error message using my 404 div.
UPDATE: A non-HTML_page.php would be any of our older, legacy pages that do not yet inherit from our "HTML_page.php" base class and all external websites that link to items on our domain that may not exist.
So, the default structure still needs the 404 routed to our 404.php page, but modify the routing if I can detect that the webpage contains my 404 div.
Here is how our .htaccess page starts out now:
# Custom 404 page
ErrorDocument 404 /eForms/404Form.php
#
# Turn on rewrite engine
RewriteEngine on
#
# Rewrite rule for document secuity
RewriteRule ^(.+)\.pdf$ /docgateway.php?source=$1.pdf [L,NC,QSA]
This snippet of jQuery's hasClass illustrates the logic I want:
if ($("div").hasClass("Error404")) {
Is there any way to build that type of logic into the .htaccess file for the ErrorDocument?

Block direct access to PHP files

My present htaccess file contains mod_rewrite rules to perform this:
www.mysite.com/articles/jkr/harrypotter --> www.mysite.com/index.php?p=articles&author=jkr&book=harrypotter
www.mysite.com/articles/jkr --> www.mysite.com/index.php?p=articles&author=jkr
www.mysite.com/articles --> www.mysite.com/index.php?p=articles
In my root directory, I have some PHP files like: index.php, pre.php, view.php, etc.
What I want: block direct access to php files.
Eg:
www.mysite.com/index.php --> "404 - Page Not Found" or "File not exist"
www.mysite.com/view.php --> "404 - Page Not Found" or "File not exist"
I have tried a code that I found on searching. It uses "f" flag. But i did not understand it. When I used it do not show the home page also. So, I removed everything that I was testing. My htaccess file now contains three rules only(to accept page, author, book).
Please help.
I saw another question that was previous asked in here : mod_rewrite error 404 if .php
A change that I made to it is, added [NC] flag. Otherwise, if we put the uppercase letter in extension(say, "index.PHP") it will load.
So,
SOLUTION:
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
thank you
Create a simple 404.php that throws a 404 error, then rewrite index.php to 404.php etc.
You can also do this at your script level. If you add a rule within your index.php to check whether you have been passed GET variables or just called the page without any parameters like so:
<?
if(empty($_GET))
header('Location: http://www.yoursite.com/404.php');
else
//your usual script
?>
Then you have control to either redirect people to a 404 error or show something more helpful, like a landing page with an index or some form of website navigation.

Categories