Error 500 on nonexistent directory - php

My .htaccess is configured with the following code so that prohackr112.tk/page1.html and prohackr112.tk/page2.php are accessible via prohackr112.tk/page1 and prohackr112.tk/page2.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
But when I try to access a directory that is really an html file (e.g. prohackr112.tk/page1/indir), I get a 500 internal server error. Why is this not simply a 404 error? If this is a problem with my .htaccess, how can I configure it so that it is a 404 error?
EDIT:
When I try to go to prohackr112.tk/page.html/indir it just gives me page.html.

You can try your rules like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Related

How can hide directory name from the url using htaccess?

For example I have url like
http://localhost/Experiments/login/login.php
I have hide the extention of file putting below code in .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.css -f
RewriteRule ^(.*)$ $1.css
RewriteCond %{REQUEST_FILENAME}\.js -f
RewriteRule ^(.*)$ $1.js
RewriteCond %{REQUEST_FILENAME}\.jpg -f
RewriteRule ^(.*)$ $1.jpg
RewriteCond %{REQUEST_FILENAME}\.ico -f
RewriteRule ^(.*)$ $1.ico
how can I make the above url to http://localhost/login
Even I tried for the solutions related to the same issue in stackoverflow but could not make it
Insert this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/Experiments/login/$1.php -f
RewriteRule ^([\w-]+)/?$ Experiments/login/$1.php [L]
# remaining rules go below this

Remove extensions for both .html and .php in apache

The code is mixed to I have .html and .php in the same folder. There is never a time there there is the same name but different extensions. I have some code that should work but doesn't. The html part works but when I try to request a PHP page without an extension the URL it gives a 404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -F
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -F
RewriteRule ^(.*)$ $1.php
You can try this on the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

Rewrite-rules issues : .htaccess

I want to make this URL clean:
news.php?post=13
but when I tried to use this RewriteRule:
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$2
It only shows the news page, no post content.
It has been bugging me for the past 2 days, and I can`t figure it out. Can someone help me with this?
Here's the entire Htaccess
RewriteEngine on
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$1
RewriteRule ^(verify)/(.*)/([a-zA-Z0-9_-]+)$ validate_account.php?usr=$2&hash=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(news)\/([0-9]+)\/?$ news.php?post=$2
use $1
Try this config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^news/([0-9]+)\/?$ news.php?post=$1
RewriteRule ^verify/(.*)/([a-zA-Z0-9_-]+)$ validate_account.php?usr=$1&hash=$2 [QSA,L]
RewriteRule ^(.*)$ $1.php
Try this method:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^news/([0-9]+)\/?$ news.php?post=$1

htaccess rewrite give error 500 in localhost

I've been searching on the web but nothing seems to work. I'm working in local with Apache and Xampp. I have created an .htaccess file and rewrote all urls ending with .php and .html to remove the extension and I have created a custom 404 page. Everything worked fine. However, now I'm trying to rewrite a dynamic url that looks like
/TotinCoblan/update/Wine?id=2
to
/TotinCoblan/update/Wine/2
and I keep getting error 500. this is my .htaccess file. From what I understood,
RewriteCond %{REQUEST_FILENAME} !-f
should avoid infinite loops.
Any help would be much appreciated.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404
Try this code:
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I finally used this code, I had to use RewriteCond %{REQUEST_FILENAME} !-f since the page was loading only after reloading two time. With that instructions loads the first time.
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ update/Wine.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
You should change your -f checks to use %{DOCUMENT_ROOT}${REQUEST_URI} instead. The problem with %{REQUEST_FILENAME} is that mod_rewrite will partially attempt to map the URI to a file, and it accounts for PATH INFO, which means if the request is something like:
/something/foo/anything/
and you happen to have an existing file/script at:
/something/foo.php
then the RewriteCond %{REQUEST_FILENAME}\.php -f check will be true, and the URI that it assumes you meant is:
/something/foo.php/anything/
And, like your code is doing, the .php is appended to the end instead:
/something/foo/anything/.php
and that causes the infinite loop. Try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404

htaccess returns Internal Server Error

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
The rewrite rule for the api/list/episode comes up with an Internal Server Error I'm not sure why but the other rule works but I have tried to change
this
http://domain/api/list/episode?season=1
to this
http://domain/api/list/episode/1
Is it is right for me to place the .htaccess in my root for all directory changes?
That is because of looping. Change that rule slightly:
RewriteEngine on
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^api/list/episode/([^/]+)/?$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Try changing the order of your rules. The two /api/ rules should go before the routing-to-php-extension rule. This is because %{REQUEST_FILENAME} actually inspects inner path elements so it could mistaken /api/list/episode/1 as /api/list/episode.php/1 as "exists", then you tack on a .php to the end, which makes it: /api/list/episode/1.php. It goes through the rewrite engine and the same thing happens again, and then the loop goes on and on.
RewriteEngine on
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can also try changing you -f check to:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f

Categories