Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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?

like image 326
tim Avatar asked Sep 14 '26 20:09

tim


1 Answers

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]
like image 189
anubhava Avatar answered Sep 16 '26 17:09

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!