.htaccess重写问题

tim*_*tim 2 php apache .htaccess mod-rewrite redirect

要么我真的需要回到课桌,要么有些奇怪的事情发生.

以下不起作用,因为实际的物理文件和目录无法解析:

<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>
Run Code Online (Sandbox Code Playgroud)

但是,这段代码可以正常工作并解析真实的文件和文件夹:

<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>
Run Code Online (Sandbox Code Playgroud)

我是否真的需要在每个RewriteRule之前使用新的RewriteCond?

anu*_*ava 6

RewriteCond仅适用于下一个RewriteRule.所以,在你的情况下,你需要在每个RewriteRule之前使用RewriteCond.

但好消息是它可以避免.

如果你想避免编写这些多个RewriteCond,你可以这样做:

## 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]
Run Code Online (Sandbox Code Playgroud)