Apache RewriteRule throwing Internal Server Error - apache

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).*)$

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.

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]

RewriteCond: unknown flag 'QSA'

When configuring Restler it suggests creating the following re-write rule:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
I have put this into a .htaccess file in root of my REST directory but I'm getting a 500 Server error when this rule is being fired. The error message the apache error log is:
[Wed Oct 10 10:39:30 2012] [alert] [client 127.0.0.1] /public/api/.htaccess: RewriteCond: unknown flag 'QSA'
[Wed Oct 10 10:39:30 2012] [error] [client 127.0.0.1] File does not exist: /public/favicon.ico
I assume the lack of a favicon.ico file can be ignored but I am concerned about the "unknown flag 'QSA'" error. I know very little about rewrite rules so any help there would be appreciated.
For those familiar with Restler, I am using 3.0.0rc2 (if that matters). Also it's worth pointing out that using the explicit call to index.php works so much as I then get a 404 JSON error response (a positive improvement) but as indicated above if I rely on the rewrite rule then I just get a 500 Server Error:
http://localhost/api/index.php/say/hi - WORKS (gives JSON 404 error)
http://locahost/api/say/hi - 500 SERVER ERROR
It doesn't look like the error has anything to do with the rules that you have:
/public/api/.htaccess: RewriteCond: unknown flag 'QSA'
which says that the QSA flag is an unknown flag for a RewriteCond, followed by a favicon.ico not found so I'm guessing that it's unrelated to requests into your REST directory.
If you had AllowOverride None, then your htaccess file is simply going to be outright ignored. There are different aspects of configuration options that can be overridden inside htaccess files, None means, htaccess cannot override any configurations, so it'll just get ignored.
The QSA flag isn't needed at all here. You only need it when you are rewritting the query string and want to append the original query string to the end. By default, the query string is appended anyways.
Interesting ... I was looking into my JSON 404 message and found this post:
Restler always returns not found
It not only solved my 404 error but also helped to fixed the 500 Server error. The only thing I needed to do was change the http.conf's AllowOverride to All.
I'd still be interested if anyone could tell me WHY this is happening but the this does now work.

How to fix infinite loop in this mod_rewrite rule?

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.