htaccess returns Internal Server Error - php

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

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

Error 500 on nonexistent directory

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]

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

How to create personalized urls in php?

I want to convert an URL like this:
http://somesocial.com/SomeSocial/success/profile?search_user=exampleuser01
Into a personalized URL like this: http://somesocial.com/exampleuser01
I've tried creating a folder with a file inside success/preview/user_preview.php
which is a copy of profile.php to get the parameter from 'search_user' with $profile_id = $_GET['profile_id'] and header('Location:'.'success/profile?'.$profile_id); To redirect the searched user for his original link.
Modified my .htaccess to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^success/preview/user_preview?search_user=
RewriteRule ^/?([^/]+)$ success/preview/user_preview?search_user=$1 [L]
This works perfectly, but every time I try to access a file from the main dir (i.e Index.php) it redirects me to http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php
Why?
Try reordering your rule wit more restrictive conditions:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([^/]+)/?$ success/preview/user_preview?search_user=$1 [L,QSA]

Php multiple mod rewrite rule conflict

I'm trying clean my url from
localhost/xxx/profile.php?name=google to
localhost/xxx/google
Below is my htaccess file
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([a-z]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If i remove RewriteRule ^(.*)$ $1.php [L] it's working properly, if i do so then url not opening without .php extension
Is there any way to use both without conflicting each other.
Ordering of rule is causing problem. Your first rule is grabbing every URI. Reverse the order and make your first rule little more smart.
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([^/]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/creatorsink/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories