RewriteRule htaccess issue - apache

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.

Related

RewriteRule [F,L] shows 500 in Apache log instead of 403

I am blocking bad bots in my htaccess file. Checking headers, I correctly see a 403 returned, but in my access logs, I see 500 instead of 403:
x.x.x.x - - [04/Aug/2020:10:56:02 -0600] "GET /testurl HTTP/1.1" 500 - "" "Yeti/1.0 (NHN Corp.; http://help.naver.com/robots/)"
Further, in Apache error log, I see:
[Tue Aug 04 10:56:02.896243 2020] [core:error] [pid 10430:tid 47402182534912] [client x.x.x.x:35589] 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.
Sample portion of my htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^.*(bot1|bot2).*$ [NC,OR]
...
RewriteRule ^(.*)$ - [F,L]
These rules are the first to be processed, before canonicalization or https redirects. I don't understand why this is resulting in "10 internal redirects" per the error log.
Thanks in advance.
I found the issue. The bad bot rewrite was redirecting to my 403 page, but since ALL files were included in the rewrite rule, that included the 403 page. At the top of the RewriteCond list of bots, I included the following, which resolved the issue.
RewriteCond %{REQUEST_URI} !^/my-403-page\.html$

If URI is a file link then prepend with folder else rewrite with index.php

Dir stracture
- dist
- js
- css
- index.php
- .htaccess
I don't want to move index.php to /dist because the dist files are build by Vue.js and excluded from git and etc.
I'm trying to get this working for hours with help of Google and Apache docs but failing.
My .htaccess for now:
RewriteEngine On
RewriteRule ^(app|dict|ns|tmp)\/|\.ini$ - [R=404]
RewriteCond %{REQUEST_URI} .(css|js)$
RewriteRule ^(.*.(css|js))$ /dist/$1 [QSA,L]
RewriteRule ^ index.php [QSA,L]
It rewrites all needed requests to the index.php, but js and css throws an error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Apache logs:
[Fri Aug 16 23:10:06.973003 2019] [core:error] [pid 10716:tid 996] [client 127.0.0.1:2106] 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.
As the title says I want to prepend all the assets with \dist folder otherwise rewrite to index.php.
I looked at backtrace and this is what I've got after changes:
RewriteEngine On
RewriteRule ^(app|dict|ns|tmp)\/|\.ini$ - [R=404]
RewriteCond %{REQUEST_URI} ^(?!dist\/?).*.(css|js|map)$
RewriteRule ^((?!dist\/).*.(css|js|map))$ /dist/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
It works
The thing is
A RewriteRule loops when the target of the rule matches the initial rewrite pattern. For example, if you were to rewrite all URLs starting with "foo" to "foo.php", then "foo.php" would also get rewritten to "foo.php", and so on, forever.
You should look in the source if you ran in to this.

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]

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.

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]