Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm currently creating a webpage which requires rewrites to look nice.

The URL is: u.{my_site}/get.php?fid={ID}.

The URL entered should be: u.{my_site}/{ID}.

Because I'm bad (and new) at rewrite regexes, I generated the following code:

RewriteEngine On
RewriteRule ^([^/]*)$ /get.php?fid=$1 [L]

Now when I try to open u.{my_site}/{ID}, it returns a 500 error and the logs state:

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.

What is causing this error and how can it be fixed?

EDIT:

Found out that the generator doesn't generate RewriteCond, so now it redirects on every page (including /get.php).

share|improve this question
    
"the generator"? You're correct, you'll need a RewriteCond directive to prevent requests for /get.php being rewritten - this is what's causing your internal redirect loop. –  w3d Nov 15 '13 at 23:57
add comment

2 Answers

As mentioned in comments above, you'll need a RewriteCond directive in order to prevent an internal rewrite loop. Whilst the L flag terminates the current rule set, the entire process is started again with the rewritten URL, so we need a get-out clause.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^fid=
RewriteRule ^([^/]*)$ /get.php?fid=$1 [L]

The 2nd %{QUERY_STRING} line prevents a rewrite loop by not rewriting if the query string is already present.

The 1st %{REQUEST_FILENAME} line prevents redirection if an existing file is requested. This is quite common when implementing "pretty" URLs, so you can request an arbitrary file if required. (As it happens this also prevents get.php being rewritten.)

Just to note, your pattern ^([^/]*)$ will not match URLs that contain a slash - there will be no match and the URL will not be rewritten. This may be desirable in your situation. However, if you wanted to match everything upto the first slash, or the entire string (if no slashes are found), then remove the end of string placeholder ($).

share|improve this answer
add comment

I actually found out (with the help of a friend) that

RewriteRule ^([a-zA-Z0-9]{3})$ get.php?fid=$1 [PT]

Is sufficient.

Please do comment when this statement contains any traps or security issues.

share|improve this answer
    
That looks good to me. You've restricted the {ID} to 3 characters (from being pretty much anything). Your substitution is obviously more than 3 chars, so no loop. However, I would query the use of the PT flag though - it seems unnecessary IMO. –  w3d Nov 18 '13 at 9:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.