tim
tim

Reputation: 2724

.htaccess Rewrite problems

Either I really need to go back to the school desk, or there is something weird going on.

The following doesn't work, as real physical files and directories do not resolve:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  # The following rule must not affect real physical files, but does
  RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

  RewriteRule .* index.php [L]
</IfModule>

However this piece of code works and resolves real files and folders just fine:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  RewriteRule .* index.php [L]
</IfModule>

Do I really need a new RewriteCond preceeding each and every RewriteRule?

Upvotes: 2

Views: 258

Answers (2)

anubhava
anubhava

Reputation: 784908

RewriteCond is only applicable to the very next RewriteRule. So yes in your case you will need a RewriteCond preceeding each and every RewriteRule.

But good news is that it can be avoided.

If you want to avoid writing these multiple RewriteCond you can do so as this code:

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

RewriteRule .* index.php [L]

Upvotes: 6

Jon
Jon

Reputation: 12992

Yes.

You can't have two RewriteRules based on one (set of) RewriteConds.

Some quotes from the manual:

Each rule can have an unlimited number of attached rule conditions...

One or more RewriteCond can precede a RewriteRule directive...

The RewriteRule directive [...] can occur more than once, with each instance defining a single rewrite rule...

Upvotes: 3

Related Questions