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
Related
How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory
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
I am trying to find answer for this for a long time.
I have created one site and i handle the not found page(404 pages) through .htaccess.
Everything works great but when i submitted to google webmaster for indexing (in fetch as google) it showing an error ( status = not found).
Does anyone know How to tell google to handle these pages (index these pages)
my .htaccess file is
ERRORDOCUMENT 404 /redirect.php
in my redirect.php file i take the necessary action.
like if URL is www.mysite.com/username then profile of that user is shown
Thank in advance
Google will never index 404 error pages.
You can use .htaccess with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /redirect.php [L]
which redirected all links to pages that do not exist
if you use:
RewriteRule ^ /redirect.php [R=301,L]
the link change in the browser. But with [L] you can show your page without link change.
First of this is my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
I've got that working but when I go to a page like example:
localhost/test
it loads the same page but shows no content.
localhost/test
is not on my server and I can't seem to get the 404 to work I've tried everything for the past 3 days
I've lost the code for the 404 in the htacces as my computer crashed
What I'm Trying to do is bring up the 404 when there isn't an id from the database so when people go to an invalid link there will be a 404 page
Just add the following code to the beginning of your .htaccess file:
ErrorDocument 404 /404.php
Then create a new file 404.php at the root of your website. This file will be displayed when the user tries to access a URL that does not match an existing file and is not handled by your Rewrite rules.
in your index.php you are doing a lookup based on the path in the url or some other variable. If you get a no match just respond with a
header("HTTP/1.0 404 Not Found");
followed by the output you want on your error document.
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