Hypertext Access file Rewriting and Redirecting Popular Rewrite Flags


Example

F|forbidden

Similar to Deny, this flag forces the server to immediately return a 403 Forbidden status code to the requesting browser or client for the request.

Example: Deny access to requests that end with exe:

RewriteRule .exe$ - [F]

G|gone

If a requested resource was available in the past, but is no longer available, you can use this flag to force the server to immediately return a 410 Gone status code to the requesting browser or client for the request.

Example: Tell a visitor that an old product no longer exists:

RewriteRule ^old-product.html$ - [G]

L|last

In most contexts, other than .htaccess, this flag instructs mod_rewrite to stop processing the current condition/rule set, much the same way last and break (Perl and C, respectively) do.

However, in the .htaccess or <Directory> context, a request that has been rewritten using a RewriteRule with this flag will be passed back to the URL parsing engine for further processing. As such, it is possible, for the rewritten URI to be handled by the same context, and perhaps altered further.

A general recommendation is to use the END flag to not only stop processing the current condition/rule set, but also to prevent any further rewriting in these contexts.

Note: The F and G flags, discussed above, both use L implicitly, so you do not need to specify them separately.

N|next

This flag will re-run the rewriting process from the beginning, starting again with the first condition/rule set. This time, the URL to match is no longer the original URI, but rather the rewritten URI returned by the last rule set. Use this flag to restart the rewriting process.

A word of warning: Use this flag with caution, as it may result in an infinite-loop!

NC|nocase

This instructs mod_rewrite to match the Pattern of a RewriteRule without being case-sensitive. To clarify, MyIndex.html and myindex.html would be regarded by the module as the same thing. Further, this flag allows you to use a-z instead of A-Za-z in a regular expression.

R|redirect

This flag is used to send an HTTP redirect response to the requesting browser/client.

By default, if no code is given, a redirect response with the 302 Found (similar to a temporary redirect) status code will be returned. If you wish to use a more permanent redirect, then you should use the 302 (301 Moved Permanently) status code.

Generally, only status codes in the range 300-399 should be used with this flag. If status codes outside of this range are used (which is perfectly acceptable), then the substitution string is discarded and rewriting is stopped as if the L flag were used. In some cases, this is a handy way to force 404 Not Found responses, even if the request points to an existing resource.

Example: Issue a 302 Found redirect response:

RewriteRule ^bus$ /train [R,L]

Example: Issue a 301 Moved Permanently redirect response:

RewriteRule ^speed-train$ /hyperloop [R=301,L]

Example: Force a 404 Not Found:

RewriteRule ^blip$ - [R=404,L]