builder-7000
builder-7000

Reputation: 7627

Apache RewriteRule [L] flag ignored

I'm trying to configure Apache rewrite rules. My website directory contains three files

index.html
index2.html
index3.html

I'm using these Apache rewrite rules:

RewriteRule index.html /index2.html [R=301,L]
RewriteRule index2.html /index3.html [R=301,L]

According to Apache's documentation about the [L] option:

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.

So when I request index.html I would expect index2.html to be served. However the request for index.html ends up in index3.html. Shouldn't the [L] option stop the processing of the second RewriteRule?

I'm using Apache version is 2.4.39 (Fedora). A similar question was asked here.

Upvotes: 1

Views: 673

Answers (1)

builder-7000
builder-7000

Reputation: 7627

I realized that the rules in .htaccess are processed on every request. A number actions take place when I access http://www.example.com/index.html :

  1. .index.html is requested
  2. The first rule in .htaccess is processed. It matches index.html and redirects to index2.html. [L] stops processing subsequent rules
  3. index2.html is requested
  4. The first rule in .htaccess is processed. It fails to match index.html so the flags [R=301,L] are not applied
  5. The second rule in .htaccess is processed. It matches index2.html and redirects to index3.html. [L] stops processing subsequent rules

I found two possible solutions:

  • Move the rewrite rules from /var/www/html/.htaccess to /etc/httpd/sites-available/example.com.conf. Unlike .htaccess which is processed on every request, example.com.conf is processed only once -- when the server is started.

  • Leave the rules in .htaccess but use [END] flag instead of [L].

The advantage of the first solution is that the rules are processed only one time.

Upvotes: 1

Related Questions