Dynamic 404 Page.. kinda - .htaccess

Alright, so this is what I am trying to achieve.
I want to be able to send URL data to a php file if the server returns 404.
for example:
example.com/stackoverflow
would bring up a dynamic 404 page
(404.php?id=stackoverflow)
but I am not using it for 404 in my case, I want to send the data after the domain.com/
So that I can pull from my database and display content accordingly.
I know this can be done with a small rewrite in the .htaccess, regex is just confusing for me.
I do NOT want a redirect.
http://example.com/datahere
should show the data of
404.php?id=datahere

You can use ErrorDocument directive to redirect 404 uris to /404.php something like the following :
ErrorDocument 404 /404.php?id=%{REQUEST_URI}
This will rewrite /datahere to /404.php?id=datahere ( /datahere will show you the contents of /404.php?id ) .
Note that the above directive doesn't work on apache versions bellow 2.4 as the mod-rewrite variable %{REQUEST_URI} is treated as plain text not as a variable on lower versions.
On apache 2.2 you could use mod-rewrite to rewrite non-existent requests to 404 . Use the following :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /404.php?id=$1 [L]

Related

.htaccess file (save request_filename) as querystring

I am using my htaccess file to forward users to a specific page when they request a notfound page. So far i have this:
ErrorDocument 404 /notfound.asp
I also want to pass the bad filename that they tried to use to this page using a querystring value. So I want something like
ErrorDocument 404 /notfound.asp?badfilename=HERE
I have been trying to play wround with URL rewrite and using %{Request_Filename} but I am new to working with .htaccess code. Is it possible to pass the bad filename to the notfound.asp with the bad file name as a querystring value? (I have code in my notfound.asp page that will retrieve the bad file name querystring value for tracking purposes.)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /notfound.asp?badfilename=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

.htaccess - 404 error

I want .htaccess to return error 404 for any url that does not meet below url pattern criteria. How can I do this in .htaccess?
Using this code in php script generates above url
$adscatcanonurl = "$script_url" . "/" . "{$vbasedir}$xcityid-$xcitynamealt/posts/$xcatid-$catname_inurl/". ("$xsubcatid"?"$xsubcatid-". RemoveBadURLChars($xsubcatname) :"");
Sample link
http://www.domain.com/17-Netherlands/posts/8-Real-Estate/
Let's say script generates a link like this
http://www.domain.com/17-Netherlands/posts/8/
As it's not matching with above url pattern, such urls should return 404 page not response.
You don't need to force .htaccess to return a 404, it will simply do so on its own if there is no match. So you can use a rule like:
RewriteEngine On
# Don't match real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-([a-zA-Z-]+)/posts/(\d+)-([a-zA-Z-]+)$ your_php_script.php?xcityid=$1&xcitynamealt=$2&xcatid=$3&xcatname_inurl=$4 [L,QSA]
Any request that isn't for an actual file (css, js, etc) and doesn't match the above rule won't match any rule, and should therefore return a 404.
Note that in the rule, I mapped the 4 components to what would be read in your PHP as $_GET['xcityid'], etc.
Why not use: ErrorDocument 404 /404page.html
http://www.yourhtmlsource.com/sitemanagement/custom404error.html

.htaccess and custom ErrorDocument for different file types

I'm wondering if it's possible to use mod rewrite along with the ErrorDocument deceleration to customize the error pages depending on what type of file is requested.
For example a non-existent html or php file is requested Apache will give nice custom HTML page.
But if a non-existent image, js, css, etc... file is requested then Apache will serve out a basic html file with only a link on it.
A good example of this behavior is Facebook. Try requesting a bogus JavaScript file, you will receive a different page then if you were to request a non-existent php file.
Ended up using a combination of both ErrorDocument and RewriteRules this works because the php page throws a 404 Not Found for me.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*(?<!.js|.css|.ico|.txt|.bmp|.gif|.png|.jpeg|.jpg)$ /error/404.php [L]
ErrorDocument 404 /404_basic.html
Use RewriteCond !-f along with a rewrite to the desired output page and a flag of R=404.
You could use a PHP script for your 404 page:
ErrorDocument 404 /error404.php
There you can analyze the URL path with PHP (see $_SERVER['REQUEST_URI']) and determine what kind of resource has been requested or is expected.

How can I get non existant files mapped correctly in .htaccess?

Duplicate:
How to rewrite non existant files to
‘default’ files?
(.htaccess)
How would I "rewrite" to a location if a file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.
So for example, let's say it is a directory with images. If the image isn't found, then it rewrites to a default image?
I.e.,
images/1.jpg
images/2.jpg
images/default.jpg
if someone tried to access "website.com/images/3.jpg",
since that doesn't exist, I want it to go to:
"website.com/images/default.jpg"
This was a previous "posted" solution, but didn't quite work:
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule /images/.* /images/error.jpg [L]
It still doesn't "get" the right image (just goes as a regular 404 request).
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^images/.* /images/error.jpg [L]
Obviously this only redirects if missing file is under /images/... but you can easily modify it for your own needs
Well, your previous posted solution is on the right track, but there's some slight craziness with it. Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule images/.* /images/default.jpg [L]
You should better send a 404 status code if the file really doesn’t exist rather than just a substitute with a status code other than 404. Otherwise the URL will be handled as valid.
So in your case I recommend you to set the ErrorDocument of the images directory to your default image:
<Directory "/path/to/your/images/">
ErrorDocument 404 /images/default.jpg
</Directory>
But note that the <Directory> block is only available in the server or virtual host configuration context and not the per-directory context (thus .htaccess).
If you cannot use the above, you could use a custom script as your custom error document to check what URL has been requested (see Request_URI environment variable) and send the default image if necessary. The ErrorDocument directive then should look like this:
ErrorDocument 404 /your-error-404.script
re-write your 404 document for your images folder:
(In your .htaccess file in your images folder)
ErrorDocument 404 default.jpg

Resources