i want to redirect to the error page if the page is not found. so i have add this code in .htaccess file.
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
but when i test it will not redirect to index.php file.
my whole .htaccess file code is here
#AuthName "Restricted Area"
#AuthType Basic
#AuthGroupFile /dev/null
#require valid-user
#<Files "/site/captcha/">
# Allow from all
#</Files>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
## File paths are relative to the Document Root (/)
# '400 Bad Request' error
ErrorDocument 400 /index.php?p=error&code=400
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
# '403 Forbidden' error
ErrorDocument 403 /index.php?p=error&code=403
# '401 Unauthorized' error
ErrorDocument 401 /index.php?p=error&code=401
# '500 Internal Server Error'
ErrorDocument 500 /index.php?p=error&code=500
############################# Pages ##########################################################
RewriteRule ^home/$ index.php [S=1]
RewriteRule ^home$ index.php [S=1]
</IfModule>
To start with -- move all ErrorDocument directives out of <IfModule mod_rewrite.c> -- there is no need at all for that.
Right now it tells Apache: "use ErrorDocument ONLY IF mod_rewrite is enabled".
If it does not work for you right now, then it is extremely likely that mod_rewrite is not actually enabled. But if you place them outside <IfModule mod_rewrite.c> block (just before it), then it should work (as long as .htaccess is recognised and processed by Apache):
#AuthName "Restricted Area"
#AuthType Basic
#AuthGroupFile /dev/null
#require valid-user
#<Files "/site/captcha/">
# Allow from all
#</Files>
Options +FollowSymLinks
# '400 Bad Request' error
ErrorDocument 400 /index.php?p=error&code=400
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
# '403 Forbidden' error
ErrorDocument 403 /index.php?p=error&code=403
# '401 Unauthorized' error
ErrorDocument 401 /index.php?p=error&code=401
# '500 Internal Server Error'
ErrorDocument 500 /index.php?p=error&code=500
<IfModule mod_rewrite.c>
RewriteEngine on
# Pages
RewriteRule ^home/?$ index.php [L]
</IfModule>
P.S.
I have also modified your rewrite rules: jointed them together into single rule.
Related
How can i change the 404 Error Page with .htaccess.
Actually, i have an htaccess file that contains rewrite rules.
In brief,i want to customize 401, 402, 403, 404 and 405 error pages.
My reputation does not allow me to comment, so i have to answer.
Example:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
#
# .htaccess content
#
ErrorDocument 403 403.php
ErrorDocument 404 /AnyDir/404.php
ErrorDocument 500 /OtherDir/500.php
The .htaccess works fine on the live server but here on my development system, it does not appear to be doing so. If I purposely enter an invalid address, it just gives the basic Apache error.
It is enabled in Apache2.conf with AccessFileName .htaccess but is there anything else that needs to be enabled to make it work? At one point it was working but many system updates later, I just noticed that it is not.
RewriteEngine On
AddDefaultCharset UTF-8
Options -Indexes Includes +FollowSymLinks
DirectoryIndex index.php
ErrorDocument 400 /messages/validate.php?error=400
ErrorDocument 401 /messages/validate.php?error=401
ErrorDocument 403 /messages/validate.php?error=403
ErrorDocument 404 /messages/validate.php?error=404
ErrorDocument 406 /messages/validate.php?error=406
ErrorDocument 408 /messages/validate.php?error=408
ErrorDocument 500 /messages/validate.php?error=500
<Files .htaccess,db_mysql.php,config.php,common.php>
Order Allow,Deny
Deny from all
</Files>
my page frequently gives errors like forbidden access or internal server error, i think the problem its because my .htaccess file configuration, here it is :
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options Indexes
ErrorDocument 400 ../errors/400.html
ErrorDocument 401 ../errors/401.html
ErrorDocument 403 ../errors/403.html
ErrorDocument 404 ../errors/404.html
ErrorDocument 500 ../errors/500.html
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.bedif\.net)(:80)? [NC]
RewriteRule ^(.*) http://bedif.net/$1 [R=301,L]
DirectoryIndex index.html
order deny,allow
deny from ../tools/
deny from ../lightbox/
deny from ../errors/
deny from ../images/
2 things:
deny from ../tools/ makes no sense at all, and all your deny from lines are causing a 500 error. The mod_auth docs say the syntax for this is:
Deny from all|host|env=[!]env-variable
Which means it can either be a "all", a host/IP, or an environmet variable. ../tools/ is none of them. You can't put paths in a Deny.
The ErrorDocument directive takes either a full URL, or an absolute path to a file. Anything else, apache assumes you're giving it a specific error message. This means, if you go to a file that doesn't exist, you'll get a 404 and the page will literally say:
../errors/404.html
as the page's message, and not the contents of the html file. Change those to absolute paths.
I am using the following code in my .htaccess:
ErrorDocument 404 error_page.php?msg=404
When I trigger a 404 error, I get a page that says this:
error_page.php?msg=404
Is there something that I am doing wrong?
Thanks
Not sure if you can pass query strings to the document designated by ErrorDocument, Something you can try is using it in conjunction with mod_rewrite:
ErrorDocument 404 /error-doc-404
ErrorDocument 405 /error-doc-405
ErrorDocument 500 /error-doc-500
RewriteEngine On
RewriteRule ^error-doc-([0-9]{3})$ /error_page.php?msg=$1 [L]
Since this is in a subdirectory off of the document root, it changes everything.
Say the subdirectory is /something/
ErrorDocument 404 /something/error-doc-404
ErrorDocument 405 /something/error-doc-405
ErrorDocument 500 /something/error-doc-500
RewriteEngine On
RewriteRule ^error-doc-([0-9]{3})$ /something/error_page.php?msg=$1 [L]
Assuming that your error_page.php file is actually in the /something/ subdirectory.
I have a .htaccess with Rewrite rules:
RewriteEngine On
RewriteRule ^admin/.*$ - [L]
RewriteRule !\.(gif|jpg|png|css|js|ico|htc|txt|swf|pdf)$ index.php
ErrorDocument 404 /404.php
#AddHandler php5-script .php
Options -Indexes
Basicly, it sends all files to index.php except the /admin/ folder and the file types above. It works just fine, but i needed to protect the /admin/ folder with .htaccess:
AuthName "Area Admin"
AuthUserFile "/path/to/folder/passwd/"
AuthType Basic
require valid-user
When i protect the folder, /admin/ stops working and starts throwing 404. If i remove it it works just fine.
Add the .htaccess in the admin folder root with your permissions. This way it will exclude itself from your main .htaccess file and ask for credentials if you were to hit that folder.