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
  • 11,643
  • 4
  • 25
  • 40
user407022
  • 21
  • 2
  • `I can't see any reason for it to "redirect"` - It's an _internal_ redirect, or more often referred to as an _internal rewrite_. This is exactly what you are doing when you "redirect" to `users.php?id=$1` etc. Do you have any other directives? Any other `.htaccess` files? Which Apache version? From the code you've posted it's not obvious why you are getting a rewrite loop. In fact, the code you posted wouldn't match a request for `/users/` (no username) so how is this being routed? Do you have a physical directory called `/users`? – MrWhite Mar 23 '17 at 13:01
  • No, no physical /users/, no other htaccess, no nothing. All it is is users.php and this htaccess provided above. And yea I know it is an internal rewrite but I can't see the reason why this document should generate 10 of them – user407022 Mar 23 '17 at 13:10
  • What does Apache error log show? – Tero Kilkanen Mar 23 '17 at 13:55
  • Get more debug info with `LogLevel warn rewrite:trace8`. – meuh Mar 23 '17 at 14:29
  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Mar 28 '17 at 11:47

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
  • 11,643
  • 4
  • 25
  • 40