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.
Related
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]
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.
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
I have recently uploaded a new site and therefore had to redirect certain pages.
There are some pages from the old site which are fine to 404, so the client can do whatever they need to do at their end.
The problem I have is that these do not throw a 404.
Example:
On the new site I have /servers and then furthermore: /servers/hp
These are in the htaccess file as: RewriteRule ^servers servers.php [NC] and RewriteRule ^servers/([^/\.]+)/?$ server.php?s=$1 [L]
However, on the old site there was a file /servers/hp-alphaserver.php - this does not exist anymore - but there is no 404, instead it shows the page for /servers
Hope that makes sense.
Found it:
RewriteCond %{DOCUMENT_ROOT}servers/([^/\.]+)/$0 !-f
RewriteRule ^servers/([^/\.]+)/[^/]+$ 404.php [L]
This checks to see if the file actually exists.
I have a website which watermarks photos (you may already know from previous q&a's). The orignal photos I use are secured using .htaccess using this code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule \.(gif|jpg|png)$ /error.gif [L]
When the user attempts to access the file it comes up with ERROR 404 which is fine but is there any way to get it to redirect to ERROR 403? (error.gif exists and is a 1x1 white pixel.)
I know .htaccess uses this to access 403 errors:
ErrorDocument 403 /error-docs/403.shtml
I have created this file in the area, and have added the above line(s) together but it still redirects to ERROR 404?
I'm not very well educated with htaccess, so any help with this will be highly appreciated.
Full Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule \.(gif|jpg|png)$ /error.gif [L]
ErrorDocument 403 /error-docs/403.shtml
Thanks,
David
To return a 403, include an F in the brackets where the L is:
RewriteRule \.(gif|jpg|png)$ - [L,F]
You can replace the /error.gif with a - which stops url rewriting since you have a separate document that gets served for the 403 so the error.gif doesn't do anything. That also means you can get rid of the RewriteCond, too.