Last Updated on May 15, 2021 by Amit

mod_rewrite END flag

END flag is used in RewriteRule to terminate the internal Rewriting process. When the rule matches the END tells server to shut down the internal Rewriting that happens only on htaccess context but not in server.config as this file is read only once by the server.
END flag is used with RewriteRules that rewrites the request internally to a destination path. You can not use END with rules that use R flag to redirect the request.
END flag is important when you are using two rules that point the request to same location as this removes the infinite loop.
END flag is supported on Apache 2.4+. You might get an internal server error for using this flag on lower versions.

Here is how END flag works in RewriteRule

Let’s see some mod-rewrite examples to understand the uses of END in mod rewrite.
The rule bellow rewrites all requests to /index.php

RewriteEngine on
RewriteRule (.*) /index.php [L]

If you use the code above in your htaccess file , and type any URL on your site you will see the contents of index.php no matter what URL , file you request.
But the main problem with the code above is that it creates an infinite loop. For example if you type /demo.php then the rule maps the request to /index.php in an endless loop something like /demo.php=>index.php=>index.pho=>index.php….. . As you can see the demo.php file redirects to the destination file and then back to itself . This will generate a 500 server error crashing your server.
Why the infinite loop
This is because htaccess file is read by server multiple times in order to process a request. When you type /deno.php your server reads the htaccess file, the rule is matched , server rewrites the request to /index.php . In order to process the destination file ‘index.php the server again reads the htaccess file , the rule is matched , index.php gets rewritten to itself causing the infinite redirect. In order to solve this you can either use END flag or use a condition for RewriteRule that tells it not to match the destination .
END flag can solve the infinite loop error

RewriteEngine on
RewriteRule (.*) /index.php [END]

The rule above works perfectly fine and does not cause a loop error because the END flag terminates the rewrite process as soon as the rule is matched and the URL rewriting from file1 to file2 is done. Server reads the htaccess file only ones when the END is present in RewriteRule.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.