The exemplified code in your question looks OK in itself, providing:
You do not have an OR
flag on the preceding condition that would make the next condition (that excludes the specific URL-path) optional.
These directives are sufficiently early in your config so as to avoid conflicts with other mod_rewrite directives. Note that the REQUEST_URI
server variable is updated in-place if you have earlier directives that rewrite the URL.
There are no conflicting mod_alias Redirect
or RedirectMatch
directives that are overriding these mod_rewrite directives. Note that the order does not matter; the mod_alias directives will execute regardless.
RewriteCond <condition 1>
RewriteCond <condition 2>
RewriteCond %{REQUEST_URI} !^/norewrite
RewriteRule ^.*$ https://example.com [L]
Note that by specifying an absolute URL (scheme + hostname) in the RewriteRule
substitution this will implicitly trigger a 302 (temporary) external redirect, not a "rewrite" as you seem to imply. If this should be a "redirect", then you should be explicit and include the R
flag - more readable and avoids potential bugs going forward.
Minor point, but you are also missing the trailing slash on the end of the absolute URL (substitution string). If you omit it then you are reliant on the user-agent appending the slash in the redirected request (all major browsers should do this).
If you only want to exclude a single URL-path then you don't need a separate condition. And since you are not currently doing anything with the RewriteRule
pattern, it is more efficient to perform this check in the RewriteRule
instead.
For example:
RewriteCond <condition 1>
RewriteCond <condition 2>
RewriteRule !^/?norewrite https://example.com/ [R=302,L]
NB: The last RewriteCond
should never have an OR
flag, otherwise the rule will execute unconditionally. (It's essentially treated like ... OR true
.)
The RewriteRule
pattern is processed first, so the above states that for all URL-paths that do not start /norewrite
then <condtion1>
, <condition2>
, apply substitution.