I removed the php directory from my URL and remove the .php extension.
But now I am trying to add my 404/500 error to make my website look nice.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /php/$1 [QSA,L]
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
When I redirect the errorDocument to an external page it works but not to my local file. I get a 500 Internal Server error. I could make a workaround and just make it go to an external page what will be my page. But that's not really clean.
Is anyone able to help me with this?
I think you must match against DOCUMENT_ROOT as well as add this RewriteCond %{REQUEST_FILENAME}\.php -f before adding .php like this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI}\.php -f
# or you could do it like this
# RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI} -f
# without php according to your setting
RewriteRule ^(.*)$ /php/$1 [QSA,L]
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
because whenever you send wrong request to any target destination you must make sure that target will handle it and there are enough conditions to classify which one should go , otherwise server will generate error.
Related
I have a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I have a file called random.php.
All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.
EDIT 1: When I use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]
then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.
Try this rule with optional trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You almost had it right, you just needed to exclude the slash as well:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]
i have this Code in my .htaccess file:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
ErrorDocument 404 https://example.com/erorrpage.php
ErrorDocument 403 https://example.com/erorrpage.php
i have one argument like this:
https://example.com/var
Your ErrorDocument is not getting triggered because of the following Rule you have in your htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
The Rule redirects everything that doesn't exist as directory !-d and !-f file to /index.php . Since mod-rewrite Rules are applied before mod-core your ErrorDocument will never get applied.
To solve this,You need to use a mod-rewrite Rule instead of ErrorDocument to Redirect non-existent requests to 404 page. So put the following at the top of your htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ - [R=404,L]
If you want to redirect 404 requests to a specific page ,you can use the following
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /errorpage.php [R= 404,L]
I will suppose that you use apache.
Ensure that your .htaccess file are on the right directory (like root directory for example) or erorrpage.php is accessible on the https://example.com/
Try to simplify it with ErrorDocument 404 "Custom very simple 404 error", just to see if it works
Ensure that your mod rewrite is enabled (on httpd.conf, uncomment the ;LoadModule rewrite_module modules/mod_rewrite.so if not. Then restart apache)
Find the relevant directory tag for your www root (on other words, on your apache config of your root directory) and change AllowOverride None to AllowOverride All
If the problem occured in a web hosting, the ensure that it allow the .htaccess
If all of these does not work, try to check apache official docs
Looking into my search console I found out some server's errors. For my surprise, I'm getting the 500 internal server error in a subdirectory that doesn't exists. BUT I have a 404 redirection that you get it on any other subdir or page that doesn't exists. Maybe that subdir of the site used to exists a few months ago but I don't see it in my server anymore. I check the .htaccess and I have all simple:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://fryla.com.ar/$1 [R,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 https://www.fryla.com.ar/404
For example, I get the error on:
https://www.fryla.com.ar/portfolio/anything
but not in:
https://www.fryla.com.ar/anyotherdir/something
https://www.fryla.com.ar/anyotherdir/
EDIT:
I follow the advice to look into the log (I don't have access buy my hosting provide me with the log). The error is:
Request exceeded the limit of 10 internal redirects due to probable
configuration error. Use \'LimitInternalRecursion\' to increase the limit if
necessary. Use \'LogLevel debug\' to get a backtrace.
But like I said, I don't have any redirect in my htaccess. Any ideas?
Tks in advance!!!
Try changing ErrorDocument 404 https://www.fryla.com.ar/404 to just the location ErrorDocument 404 /404. removed an extra RewriteEngine On as only 1 is needed
copy the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://fryla.com.ar/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 /404.html
EDIT: update to error document location
I found the solution... this used to be my .htaccess for remove the php extensions of every page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Now, I found another code for doing it and the error is gone. The new code is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Tks to you all for the help!
I know what this error is, but I'm sure it has something to do with my recent change of .htaccess or something missing from it. I added a few lines of rewrite code to change "something.php" to "something" - it worked fine. When testing my website online for the last few stages of development, I wanted to see what type of errors I'll get by attempting to access folders, and files - 404, 403, 401 etc.
Lets get to the point! When I entered ....com/press or ....com/press.php it will take me to the page as expected. When I enter ...com/press/ I get a 500 error - yes it has a slash, but shouldn't I be getting a 404 error? I say this because the file press.php exists, but not the folder ...com/press/ - it doesn't even show my own custom error page!
After taking a look at what I put in this .htaccess file, I also want some additional help with .htaccess. Index should be index alone, the server shouldn't accept .php even if it is, if you know what I mean!
Thanks in advance
Code in .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Modify your rule as:
ErrorDocument 404 default
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Index should be index alone, the server shouldn't accept .php even if
it is
You can use this so that if they using the php extension, it will redirect.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.+)\.php
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ /$1.php [L,QSA]
Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.
Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase