How to fix infinite loop in this mod_rewrite rule? - apache

I've got the following in an .htaccess. file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/css(/?|/.+)$
RewriteCond %{REQUEST_URI} !^/js(/?|/.+)$
RewriteCond %{REQUEST_URI} !^/img(/?|/.+)$
RewriteRule !^/handle\.php$ /handle.php [L]
I simply want all requests (except those beginning with /css/, /js/, and /img/) to be sent to handle.php.
But when I make a request I get a 500 error, and this is printed in the error log:
[Sat Mar 24 16:14:53 2012] [error] [client x.x.x.x] 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.
Why is it getting in an infinite loop?

It's probably that rewriting a request to /handle.php does not match !^/handle\.php$, so it tries to rewrite the rewrite, etc. Your issue might be the leading slash? Try playing around with that.
If not, check out the RewriteLog directive (and see the entry just below that, RewriteLogLevel). You'll be able to see what rewrites are being attempted, and you'll likely be able to figure out what's going wrong.

Related

RewriteRule htaccess issue

i have live-cams.php file in root directory of my project and i try to rewrite link as /live-cams
RewriteRule ^live-cams ./live-cams.php [NC,L]
This rule throw
500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
also throw the same error when i try to open file /live-cams.php. Rule for index.php looks like to live-cams.php and it work well
RewriteRule ^home ./index.php [NC,L]
When i type friendly link as ^live-cams/ instead of ^live-cams it works.
Apache error log:
[Fri Aug 18 12:20:38.011915 2017] [core:error] [pid 14024:tid 1920] [client ::1:58474] AH00124: 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.
Anyone know whats is wrong?
You need to use anchors in your regex pattenrs:
RewriteRule ^live-cams/? live-cams.php [NC,L]
RewriteRule ^home/?$ index.php [NC,L]
Otherwise live-cams is part of your original and rewritten URIs thus causing mod_rewrite to keep executing your rules until it hits LimitInternalRecursion count.

Apache RewriteRule throwing Internal Server Error

I'm using Apache 2.4.18 with a self-signed SSL certificate (working OK) in a local virtualhost, and using this .htaccess configuration throws HTTP 500 error:
Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^admin/users/edit/([^/]*)$ /admin/users/edit/edit.php?user=$1 [QSA,L,NC]
The error is shown only in the "edit page", not in every page.
https://mydomain.dev/admin/users/edit/3
I've use the same rewrite logic previously without any conflict, so I can't identify the error.
The Apache SSL error log for that virtualhost has this log:
[core:error] [pid 1257] [client 127.0.0.1:50669] AH00124: 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., referer: https://mydomain.dev/admin/users/
What may be the issue?
You are getting a rewrite loop error. The problem is with your Rewrite pattern ^admin/users/edit/([^/]*)$ as it also matches the Rule's target admin/users/edit/edit.php . You need to exclude the destination path from your rule, put the following Condition above your RewriteRule line :
RewriteCond %{REQUEST_URI} !^/admin/users/edit/edit\.php$ [NC]
You can also fix it using a negitive lookahead based regex pattern :
^admin/users/edit/((?!edit\.php).*)$

URL Rewrite in .htaccess causing internal 500 error

SOLVED: I've been having an .htaccess rewrite problem with rare cases causing internal 500 errors on my site.
I setup a Rewrite rule in my .htaccess to redirect
http://mysite123.com/churches/ANY_NUMBER_HERE/any_name_here
to
http://mysite123.com/churches.php?churchid=ANY_NUMBER_HERE , but the end-user will still see http://mysite123.com/churches/ANY_NUMBER_HERE/any_name_here
It works perfectly unless they type http://mysite123.com/churches/ANYTHING_BUT_NUMBERS_HERE or if they type numbers without a trailing slash.
This is my rewrite rule:
RewriteRule ^churches\/([0-9]+)\/.*$ /churches.php?church=$1 [L]
Basically, is there a way I can change my rule/regex to make the htacess point to a 404 error page if they type any letters, special symbols, or numbers WITHOUT a trailing slash? I need to avoid the 500 internal server error, it's hurting my SEO
EDIT: I'm getting this error code in the server logs:
[Tue Aug 04 20:48:44 2015] [error] [client 24.107.131.201] 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.
EDIT #2: I solved the problem by adding this rewrite rule that stops loops:
#STOP LOOPS
RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

mod-rewrite remove folder name from url

I am using OJS and I have installed it inside a folder called "journals". I have created some journals there (for example "journal1", "journal2", etc).
Now I am getting paths like this: www.example.com/journals/index.php/journal1,
www.example.com/journals/index.php/journal2, etc.
What I want is to map www.example.com/journals/index.php/journal1
to looks like www.example.com/index.php/journal1 (removing the journal part from URL).
I can't move OJS to root because I have some other files there.
Here is the .htaccess file which I am using currently (it's in "journals" folder and giving me a 500)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^journals
RewriteRule ^(.*) /journals/$1 [L]
</IfModule>
Also Here is the error.log
[Fri Oct 12 22:16:45 2012] [error] [client 127.0.0.1] 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.
When you put those rules inside the htaccess file in your /journals directory, it's causing a rewrite loop. $1 never starts with journals because you're inside the journals directory, thus the rule keeps getting applied.
You'll need to put something like this in your htaccess file in your site-root, the / directory:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/journals
RewriteRule ^index\.php(.*)$ /journals/index.php$1 [L]

mod_rewrite To Direct Subdomain Access

We are setting up an API and we want to use Apache mod_rewrite to direct all accesses to http://api.domain.com to the script located at /cgi-bin/api.pl. Here is what we're currently using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^(.*)$ /cgi-bin/api.pl [NC,L,QSA]
However, this is giving us the following error:
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] 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.
If we try to access http://domain.com/cgi-bin/api.pl the script functions properly. What are we doing wrong? Please help! Thanks in advance.
I assume that you're defining your rules in a .htaccess file, where the L flag might not work like you were expecting.
Since your test pattern ^(.*)$ matches whatever input is given to it, the URL you rewrite to is also matched on the subsequent request, and you get an internal infinite redirection loop (resulting eventually in the server error).
A solution is to check to see if you've already rewritten the URL:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/cgi-bin/api.pl
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^.*$ /cgi-bin/api.pl [NC,L,QSA]