2

I'm quite new to .htaccess and I just didn't get into it enough to understand what's going on. The server just says, limit of 10 internal redirects exceeded even though I can't see any reason for it to "redirect". It only happens when I try to use /users/Username, /users/ shows an error that this user can't be found which is like I wanted it, /users/Username throws a 500 Internal Server Error. The document itself is fine, the database check should also work. The .htaccess document is:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

IndexIgnore * 

ErrorDocument 400     /
ErrorDocument 404     /

RewriteRule    ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1    [NC,L]
MrWhite
  • 13,315

1 Answers1

1
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
:
RewriteRule    ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1    [NC,L]

The first rule block is catching a request for /users/Username. In this instance, REQUEST_FILENAME is .../users (the first whole path segment after a valid directory) and .../users.php is indeed a valid file in this case. However, you are appending .php onto the end of the requested URL, so /users/Username becomes /users/Username.php, /users/Username.php.php, etc.

You can get around this conflict by simply reversing the order of these directives:

RewriteRule ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1    [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

Now, a request for /users/Username will be caught by the first rule and correctly routed to user.php?id=Username, which will fail to match the second rule block.

(I removed the NC flag since you are already checking for a-zA-Z in the pattern. Unless /users could be /uSeRs as well?!)

MrWhite
  • 13,315