Search code examples
phpapache.htaccessmod-rewriteclean-urls

Error in htaccess file when clean url of php file


When I open this page of my website it gives a 500 error:

single-portfolio/3/bizdb-business-directory-data-scraping-uk-

My .htaccess file:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteRule ^single-portfolio/([0-9]+)/([0-9a-zA-Z_-]+)$ single-portfolio.php?id=$1&title=$2 [NC,L]

Error shown in browser is:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at postmaster@localhost 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.

error log reports:

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


Solution

  • RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [NC,L]
    

    It's this rule (that appends the .php extension via an internal rewrite) that is incorrect.

    This results in a rewrite-loop when requesting /single-portfolio/<anything> because /single-portfolio.php exists (satisfying the condition) but this rule will incorrectly rewrite the request to /single-portfolio/<anything>.php, which would ordinarily result in an erroneous 404 (since you are expecting the last rule to catch this request), but due to the L flag the rewriting engine starts over and repeatedly rewrites the request to /single-portfolio/<anything>.php.php to /single-portfolio/<anything>.php.php.php etc. etc. until the server "breaks" and a 500 response is returned.

    See my answer to the following question on ServerFault with a more detailed explanation of this behaviour: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

    Simply changing L to END will fix the rewrite-loop, but won't resolve the overall problem because you'll just get the erroneous 404 as mentioned above (the request won't be correctly rewritten).

    Changing the order of your directives so the last rule that rewrites ^single-portfolio/ is before your second rule that appends the .php extension (via an internal rewrite) would solve your immediate problem, however, you need to fix the above rule so you are testing the same file you are ultimately rewriting to (otherwise, any URL of the form /php-file-that-exists/<anything> would result in a rewrite-loop / 500 Internal Server Error response, instead of the expected 404).

    For example:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [L,R]
    
    RewriteRule ^single-portfolio/(\d+)/([\w-]+)$ single-portfolio.php?id=$1&title=$2 [NC,L]
    
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]
    

    NB: The shorthand character class \w is the same as [0-9a-zA-Z_]. And \dis the same as[0-9]`.

    The NC flag on the first and last rules are entirely superfluous. I would be wary of using the NC flag on the ^single-portfolio/ rewrite (it shouldn't be necessary), as this potentially enables duplicate content (eg. /single-portfolio/... and /SiNgLe-PoRtFoLiO/... are both valid and resolve to the same resource.)

    The first rule that removes the .php extension via an external redirect should ultimately be a 301 (permanent) redirect once you have confirmed everything is working OK.