More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Saturday, April 1, 2017 9:42:25 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 521

.htaccess - multiple RewriteRules and too many redirects error

After mofifying .htaccess, I got the Error 310 (net::ERR_TOO_MANY_REDIRECTS): too many redirects with any browser.

The cause was: I put multiple RewriteRules under same conditions in .htaccess file.

If the conditions are met / matched, the first rule is applied and ends the rule processing because of RewriteRule flag [L]. However, if not matched, the first rule is skipped and the next rule is processed. But we have two domains (one primary and the other subdomain). This would cause the above mentioned error if RewriteCond is not specified for the second rule. See below.

Code:

#two RewriteRules under the same RewriteCond 
#RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(www.)?bettereducation.com.au$
RewriteRule ^login\.aspx(.*)$ https\://bettereducation\.com\.au/login\.aspx$1 [NC,R=301,L]
RewriteRule ^paypal/CheckOut\.aspx$ https\://bettereducation\.com\.au/paypal/CheckOut\.aspx [NC,R=301,L]


So rewrite conditions only affect the following rewrite rule.

I have to separate the two rules and repeat my RewriteConds. It's OK to have multiple conditions. The conditions are combined with logic AND. If you want a combination with logic OR, add an RewriteCond flag [OR]. The issue here is not the combination of conditions, but the rules. The rules can be combined by your design or RewriteRule flags [C] or [N].

Quote:
The [C] or [chain] flag indicates that the RewriteRule is chained to the next rule. That is, if the rule matches, then it is processed as usual and control moves on to the next rule. However, if it does not match, then the next rule, and any other rules that are chained together, are skipped.

The [N] flag causes the ruleset to start over again from the top, using the result of the ruleset so far as a starting point. Use with extreme caution, as it may result in loop.

The [Next] flag could be used, for example, if you wished to replace a certain string or letter repeatedly in a request. The example shown here will replace A with B everywhere in a request, and will continue doing so until there are no more As to be replaced.

RewriteRule "(.*)A(.*)" "$1B$2" [N]
You can think of this as a while loop: While this pattern still matches (i.e., while the URI still contains an A), perform this substitution (i.e., replace the A with a B).

In 2.4.8 and later, this module returns an error after 32,000 iterations to protect against unintended looping. An alternative maximum number of iterations can be specified by adding to the N flag.

# Be willing to replace 1 character in each pass of the loop
RewriteRule "(.+)[><;]$" "$1" [N=64000]
# ... or, give up if after 10 loops
RewriteRule "(.+)[><;]$" "$1" [N=10]


If we cannot combine the rules, we need to separate the rules with duplicated conditions.

Code:
#RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(www.)?bettereducation.com.au$
RewriteRule ^login\.aspx(.*)$ https\://bettereducation\.com\.au/login\.aspx$1 [NC,R=301,L]

#repeat conditions
#RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(www.)?bettereducation.com.au$
RewriteRule ^paypal/CheckOut\.aspx$ https\://bettereducation\.com\.au/paypal/CheckOut\.aspx [NC,R=301,L]


Note:

NC - Not Case sensitive
R - Redirect
R=301 - Redirect permanently

L - Last
Quote:
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

An alternative flag, [END], can be used to terminate not only the current round of rewrite processing but prevent any subsequent rewrite processing from occurring in per-directory (htaccess) context. This does not apply to new requests resulting from external redirects.


Related:

1. RewriteRule Flags
RewriteRule Flags with detailed explanations and examples

2. Apache Module mod_rewrite and Rewrite Rules
Apache Module mod_rewrite Rewrite Rules
Apache Module mod_rewrite documentation with detailed explanations and examples

3. Multiple RewriteRules for single RewriteCond in .htaccess
You can use the RewriteRule flag S|skip to tie multiples RewriteRules to a single RewriteCond (or to a set of RewriteConds).
http://stackoverflow.com...-rewritecond-in-htaccess

4. Two redirect rules conflict with each other .htaccess
http://stackoverflow.com...with-each-other-htaccess

Edited by user Sunday, April 2, 2017 12:03:17 PM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.