I want to use htaccess to show 404/401/400 ... error messages but my website always shows errors which CI default error messages. I dont want to use CI error function or custom it. Is the anyway to disable that ?
I tried this in my htaccess file but not working.
RewriteEngine on
ErrorDocument 404 /404.html
One solution is to use a rewriteRule for your error pages.
RewriteRule is faster then the ErrorDocument directive.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /404.html [NC,L]
This will rewrite all non existent dirs and files to /404.html
Related
Basically, I've got the following .htaccess file:
Options -Indexes
ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=400
ErrorDocument 403 /index.php?error=400
ErrorDocument 404 /index.php?error=400
ErrorDocument 500 /index.php?error=400
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Enable sites without extentions
options +MultiViews
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Adaptive-Images -----------------------------------------------------------------------------------
# Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
# RewriteCond %{REQUEST_URI} !ignore-this-directory
# RewriteCond %{REQUEST_URI} !and-ignore-this-directory-too
#RewriteCond %{REQUEST_URI} !img
# don't apply the AI behaviour to images inside AI's cache folder:
#RewriteCond %{REQUEST_URI} !ai-cache
# Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
# to adaptive-images.php so we can select appropriately sized versions
# RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php
# END Adaptive-Images -------------------------------------------------------------------------------
# SEO Friendly Redirect Rules -----------------------------------------------------------------------
RewriteRule ^(admin)/?$ admin/ [L,NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]*)/?$ index.php?title=$1&ch=$2 [L]
RewriteRule /$ index.php [END]
As of right now, the rules and conditions all work great except for one thing. If I navigate to say: "example.com/nonexistingfolder" , it is using this rule to process:
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
Which brings up a php error because the argument passed is invalid. Which is to be expected, because it doesn't exist. But if I no navigate to: "example.com/nonexistingfolder/index.php" , I'm correctly greeted with my 404 page defined at the top. (don't worry about the slashes, in the original these are permalinks)
ErrorDocument 404 /index.php?error=400
I don't know too much outside of what I've learned today about .htaccess and mod_rewrite. Is there some other way I should be going about this? Or am I missing other statements?
I've tried adding the following lines to my .htaccess with no progress:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
Either these don't help in my case (and I thought the second would for sure), or I am still doing something wrong.
I expected the code to be rather straightforward, however this has left me scratching my head for a couple of hours. Thanks for taking the time to read and ponder this with me! Have a good one.
EDIT: I added these three lines of code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php?error=400 [L]
Which works when I navigate to something that doesn't exist. Yet, it interrupts the other RewriteRule I have set: RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
So now I am for sure at a loss at what to do.
To further exemplify:
Say I navigate to "website.com/nonexistingfolder". Which now because I added the three lines above, shows the 404 page correctly. However, because of the single RewriteRule above, if I navigate to: "website.com/indexquery" it will also show the 404. How can I tell my .htaccess to differentiate between the two?
I'm using CodeIgniter 3.1.1 .
The problem is when i browse sub-directory(http://www.wap.com/demosite) where CodeIgniter installed, it always redirect to main domain and i think the problem is in .htaccess.
For example :
Domain : http://www.wap.com
Main site directory public_html/
And .htaccess file in main-directory public_html/.htaccess
RewriteEngine on
RewriteOptions inherit
ErrorDocument 500 /errors/404
ErrorDocument 401 /errors/404
ErrorDocument 403 /errors/404
ErrorDocument 404 /errors/404
RewriteCond %{HTTP_REFERER} !^http://wap.com/.*$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|js)$ - [F,NC]
CodeIgniter installation directory
public_html/demosite/
And .htaccess file in sub-directory public_html/demosite/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
I'm assuming the move is a new addition?
Need to make sure that in application/config/config.php that your base_url is set to http://www.wap.com/demosite not to http://www.wap.com
This is often missed and causes these kinds of redirects
Your codeigniter htaccess should also be set to RewriteBase /demosite/
I edited my htaccess file to rewrite mysite.com/page to mysite.com/page.php and to show 404 error page if a user requested mysite.com/page.php instead of mysite.com/page
I also added my own custom 404 error page
But when I requested mysite.com/page.php, the error page shown was Apache default 404 error page (instead of my custom error page), with additional error info:
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My error log:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
Here is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
ErrorDocument 404 /error/404
I have tried changing 404 at the last line above to 404.php but the custom error page still not showing up..
However, the custom error page did show up when I requested mysite.com/page/ (notice the slash).
I don't understand, where this error comes from. But you can resolve it by exiting the rule chain when 404.php is requested. Insert this rule at the beginning
RewriteRule ^error/404.php$ - [L]
Unrelated, but you can simplify your rules a little bit
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php$ - [L,R=404]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php
ErrorDocument 404 /error/404.php
I need help in php. I am making one portal website in Core Php. I had created 404 page and .htaccess file.
404.php:
<html>
<head>
<title>404 Error - Page Not Found</title>
</head>
<body>404 Error - Page Not Found!</body>
</html>
.htaccess:
Redirect 404 /404.php
But this is not working. If I use this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
It shows internal server error.
Here is my website: http://jobguide.australianenglishcenter.com/
ErrorDocument 404 http://example.com/404/
add this line to htaccess and change the url
Try this in your .htaccess:
ErrorDocument 404 http://jobguide.australianenglishcenter.com/404/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ <YourRelativePathToPHPFile>/404.php [L]
Here,The ErrorDocument redirects all 404s to a specific URL
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.
NOTE: Replace
the htacces should look like
ErrorDocument 404 /page404.php
You have to set the rewrite engine on. Then customize a php page to handle the 404 error. Refer the below code
RewriteEngine on
ErrorDocument 404 http://www.example.com/your-404-custom-file-name.php
call the function :
http_send_status(404);
you can also use below to make all 404 requrest redirect to specific page
FallbackResource /index.html
You can always use the method http_response_code(int code).
Here is the method documentation.
You can try this code in 404.php in which you can create your own error page...
<?php header('Location: /directory/page-name/'); ?>
Open Your htaccess and insert this
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
ErrorDocument 404 http://www.yoursite.com/404.php
This works for me. I don't use any CMS, and my website in php too.
I think a simple way to add a 404, 505, or other pages in your project just put the simple thing in your .htaccess file which is in the project root folder and paste the simple line
RewriteEngine on
errordocument 404 http://localhost/MVC_Project2/404.php
I want to redirect to the requested file name so if someone types
www.example.com/user/1
it should redirect to the user.php file with 1 lets so someone type
www.example.com/apple/3 or www.example.com/apple which does not exists but it should redirect still redirect to apple which will auto give 404
I have no idea on how to do this i found this code here it works but redirects everything to index.php even if i type apple
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /SimpleMVCTest/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]
Edit:
Little more detail
can RewriteRule ^(.*)$ index.php?get=$1 [L] be dynamic so instead of index.php it gets the requested one from the url?
so if a user types www.example.com/{requested URL?something=something}
then i want .htaccess to redirect to {requested URL/something/something} and all to be dynamic to if a user types cars or pear or rabbit/3 it would redirect to that page without it being hard coded in htaccess like rabbit.php?someting=someting i want every thing to be dynamically so something like this RewriteRule ^(.*)$ {requested URL?something=something}?get=$1 [L]
Edit 2:
OK after a lot Google/Youtube/Stackoverflow research and putting random things together I finally got something that partly works here:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /SimpleMVCTest/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to REQUESTED_FILENAME
RewriteRule ^(.*)$ $1.php?get=$1 [L]
There is two issues I have noticed with this
1: Giving www.example.com/car/1 will result in 500 Internal Error but if i write www.example.com/car it works fine. How do i make it so i can go car/1 and it will not give 500 internal Error
2: if i write www.example.com/somethingthatdoesnotexist or www.example.com/somethingthatdoesnotexist/1 it gives 500 internal but should just redirect to 404 but it's not found
How would I do this?
In a .htaccess file use the following:
Options -MultiViews
RewriteEngine On
RewriteRule ^user$ user.php
RewriteRule ^user/$ user.php
RewriteRule ^user/([a-zA-Z0-9\-\_]+)$ user.php?user=$1
If you use as a link now something like profile/username it will show that users profile.
It may mess up some css an js files, if it does on the page in the head use:
<base href="/" />
This should correct any css/js/image links