【问题标题】:.htaccess Rewrite problems.htaccess 重写问题
【发布时间】:2013-07-22 07:34:36
【问题描述】:

要么我真的需要回到课桌,要么发生了一些奇怪的事情。

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

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

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

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

我真的需要在每个 RewriteRule 之前都有一个新的 RewriteCond 吗?

【问题讨论】:

    标签: regex apache .htaccess redirect mod-rewrite


    【解决方案1】:

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

    但好消息是它是可以避免的。

    如果你想避免编写这些多个 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]
    

    【讨论】:

      【解决方案2】:

      是的。

      您不能基于一个(一组)RewriteConds 拥有两个 RewriteRules。

      来自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...

      【讨论】:

      • voteup 非常感谢您的回答。我会接受 anubhavas 的回答,因为它提供了一个很好的解决方法示例。但我想与大家分享我的感激之情。
      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2015-02-24
      相关资源
      最近更新 更多