Mobile app version of vmapp.org
Login or Join
Rivera981

: Rewrite using .htaccess results in too many redirects 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:

@Rivera981

Posted in: #Apache2 #Htaccess #ModRewrite

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= [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).

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Rivera981

2 Comments

Sorted by latest first Latest Oldest Best

 

@Rivera981

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

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

Is sufficient.

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

10% popularity Vote Up Vote Down


 

@Alves908

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= [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 ($).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme