Have “Redirect permanent” take precedence over mod_rewrite in .htaccess

apache-2.2mod-rewriteredirect

I have organized my huge .htaccess file by Redirect permanent first which I would like to have executed if found. In those case, I don't want later mod_rewrite processed.

Is there any way to achieve that?

Examples:

Redirect permanent /products/example.aspx http://www.example.com/products/example/

RewriteCond %{REQUEST_URI} products/([1-9a-zA-Z\s-]+)/
RewriteRule ^products/([0-9a-zA-Z\s-]+)/tutorials(/|\.php)$ /products/$1/help/ [R=permanent,L]

While above does not conflict, my .htaccess file is 60k large, and there are some simple "Redirect permanent" that seems to be ignored because a later RewriteRules (that e.g. redirects a pattern instead)

Best Answer

Putting "Redirect" statements before "RewriteRule" in a .htaccess file does not mean they get executed first. RewriteRules get executed before Redirect and Alias directives. They are handled by different modules, and mod_rewrite gets called first. For this reason I sometimes use a rewrite to short circuit all alias and redirect statements.

I would suggest not to mix redirects and rewrites, and to convert your redirects to rewrites and add them to the head of your list or rewrites.