Internal Server Error when file or folder does not exists - apache

I use this rewrite code to rewrite all requests to .php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
So when I enter index the index.php will load or if I enter contact the contact.php will load.
But when I enter a name which does not exists I will get Internal Server Error, for example I entered this name test or fdgdfg/ewrtt/ddfghdfg, after this I will get this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#mydomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
I have ErrorDocument 404 error.php too in my .htaccess.
this is my complete .htaccess file :
ErrorDocument 404 index.php
RewriteEngine on
RewriteBase /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]

You need to check that the php file actually exists, otherwise the 2 conditions that you have will always fail and the .php extension keeps getting appended, e.g. you'd end up with a URI looking like:
/shop/foo.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php
until the internal recursion limit is reached and you get a 500 error. Try adding another condition to check if the file exists:
ErrorDocument 404 index.php
RewriteEngine on
RewriteBase /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [L]

Following solved my problem -
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]

Related

how to change url to a clean url that works

I've been looking for an answer for my problem, however the things I tried didnt work out. What I've been trying to do is to create a beatiful url for this link:
mywebsite.com/blog_template?slug_url=blog-post-name
to
mywebsite.com/blog-post-name
To achieve this I tried the following code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_template/([^/]+)$ /blog_template?slug_url=$1 [L]
But my code didnt work... Any advice?
Here’s the full htaccess my website:
RewriteEngine On
RewriteBase /
ErrorDocument 404 http://91.218.67.117/404/
ErrorDocument 500 http://91.218.67.117/500/
#redirect 404
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
#remove php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Considering you want to rewrite this request in backend to index.php(OR change it to appropriate file's name in case its some other php file). With your shown samples and attempts please try following .htaccess Rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog_template\?slug_url=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?slug_url=$1 [QSA,L]
ErrorDocument 500 /pages/errors/500.php
NOTE: Please keep your .htaccess file and index.php(OR any php file which is taking rewrite request in backend for that matter) in same path(your root etc).
Your attempt was close. Just a small fix:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
This relies on additional rewriting rules to get applied though, since /blog_template most likely is not a resource the http server can somehow "execute" immediately. So you may want to combine above rule with other rules, which you did not reveal to us, which is why I cannot say anything about that.
If you also want to redirect requests to the "old" URL, then that variant should do:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug_url=([^/]+)$
RewriteRule ^/?blog_template$ / [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
It is a good idea to start out using a R=302 _temporary_redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.

mysite.com/ returns 403 error when using RewriteRule to remove php file extensions

I'm attempting to hide .php file extensions from URLs using the following piece of code in my virtual host's conf file (apache2). Works great, except for when a user attempts to load just the domain (www.mysite.com). Then a 403 error is returned.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Please advice.
The problem was that it redirected root / to /.php. Fixed by exluding root folder
RewriteCond %{REQUEST_FILENAME} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Internal server error when no file specified or file is missing

I am using this rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^img.domain.com [NC]
RewriteRule ^(.*)$ data/photos/$1 [L]
Its doing that, when I use f.e http://img.domain.com/profile_image.jpg it will show content of file http://www.domain.com/data/photos/profile_image.jpg without changing url (masked). This is ok.
The problem is, just in case, when somebody enter http://img.domain.com, it will return 500 Internal server error, but I want it to show 403 Forbidden, the same as when you enter http://www.domain.com/data/photos/. I am helpless, how to modify this rewrite rule to achieve this?
EDITED:
Also the same issue is when the file doesnt exist in /data/photos/, then I want to return 404... but its returning 500.
In errorlog is too many recursions...
You should first check whether destination file exists and then rewrite it:
# 403 for img subdomain when request is for landing page
RewriteCond %{HTTP_HOST} ^img\.domain\.com$ [NC]
RewriteRule ^(data/photos)?/?$ - [F,NC]
RewriteCond %{HTTP_HOST} ^img\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/data/photos/$1 -f
RewriteRule ^(.+)$ data/photos/$1 [L]

Remove trailing .html rewriterule creates 404 error

I have modified .htaccess to remove trailing .html from my pages. htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.mydomain.co.uk} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.mydomain.co.uk/$1 [R=301,L]
I also added the following at the top of the .htaccess to set up a custom 404 page and stop indexing:
ErrorDocument 404 /notfound.html
Options -Indexes
The rewrite rule works perfectly. So does the anti-indexing. However the 404 page gives me this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#mydomain.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
If I remove the rewriterule then the 404 page works fine. I don't know a lot about this kind of stuff and was wondering what to do to be able to get both aspects to work correctly? Can anyone help please?
(PS: I read this answer and think it might apply to me - but didn't understand it at all.)
Just wanted to let you know I found a solution for this:
ErrorDocument 404 /notfound.html
Options -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.htm\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.htm$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L]
Works great!

How Apache mod_rewrite Module works?

I'm trying to make my own MVC PHP framework using a tutorial that i found over the net .
but i got a problem understanding the rewrite_mod in the htaccess files. here is the first part :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
1) As written in the tutorial those rules will redirect all the request to the public folder , so the first question is why we have two rules ? what means the first and the second. one
2) the second part is another htaccess file in the public folder containing :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
the second part will rewrite the url to index.php?url=$1 this part is clear but this part is little bit difficult for me
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
i learn about it and it tell that the request should not be a file or directory but the index.php is a file (in public directory) .
anothe question please why when we remove the last .htaccess file (in public directory) we
got this error :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#127.0.0.1 to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
and when we have only a htacces containting only this part ,it works great ?
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Thanks so much .
You can combine and fix the root .htaccess:
RewriteEngine on
RewriteRule !^public/ public%{REQUEST_URI} [L]
This means forward to /public/<uri> if REQUEST_URI does not start with /public
Some explanation now.
DOCUMENT_ROOT/.htaccess:
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
1st rule is internally forwarding to public/ with request URI is empty i.e. http://site.com/
2nd rule is internally forwarding even URI to public/<URI>
DOCUMENT_ROOT/public/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
Here inside public direcotry:
RewriteCond %{REQUEST_FILENAME} !-f means if request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d means if request is not for a valid directory
RewriteRule ^(.*)$ index.php?url=$1 means forward request to index.php?url=<uri>
Reference: Apache mod_rewrite Introduction