ngplayground
ngplayground

Reputation: 21617

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?

Upvotes: 1

Views: 87

Answers (2)

Jon Lin
Jon Lin

Reputation: 143856

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

Upvotes: 1

anubhava
anubhava

Reputation: 784908

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]

Upvotes: 1

Related Questions