tru7
tru7

Reputation: 7222

RewriteCond scope in htaccess

I want to redirect non-existing files with versioned filename to the current version. There are two target files depending on a pattern in the requested filename.

For example the current file is setup-win-3-1-0.exe A request for setup-win-2-7-1.exe (non existent) would redirect to the previous one. (And setup-mac-2-7-1.dmg would go to setup-mac-3-1-0.dmg)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^setup-win-[^.]+\.exe$ setup-win-%{ENV:DOWNLWIN}.exe [L,R=301,NC]
RewriteRule ^setup-mac-[^.]+\.dmg$ setup-mac-%{ENV:DOWNLMAC}.dmg [L,R=301,NC]

(The ENV:DOWNL is set before, omited for clarity)

I've been searching the web and I find some contradictions, I've seen that RewriteCond applies only to the next RewriteRule and in other places that it applies until it fins a RweriteRule with Last flag.

Upvotes: 1

Views: 145

Answers (1)

anubhava
anubhava

Reputation: 785481

Would that RewriteCond apply to both lines?

Answer is No. RewriteCond is only applicable to the nearest RewriteRule. It is never applied to more than one rules defined using RewriteRule directive.

Alternative Approach: If you don't to want RewriteCond over and over again before all the rules then have a separate **skip-all* rule using opposite of the RewriteCond. Consider below example:

RewriteEngine 

# skip all the rules *below* of comdition is met
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^setup-win-[^.]+\.exe$ setup-win-%{ENV:DOWNLWIN}.exe [L,R=301,NC]

RewriteRule ^setup-mac-[^.]+\.dmg$ setup-mac-%{ENV:DOWNLMAC}.dmg [L,R=301,NC]

Upvotes: 2

Related Questions