现有文件夹的mod_rewrite条件

时间:2012-07-24 10:49:51

标签: apache mod-rewrite

我正在尝试设置 .htaccess 文件,该文件会将每个请求网址GET传递到名为index.php的文件中。唯一的例外是,当请求URL指向目录 res

示例:

/wiki/cool-stuff          => index.php?uri=/wiki/cool-stuff
/wiki/cool-stuff?news=on  => index.php?uri=/wiki/cool-stuff&news=on
/res/cool-photo.jpg       => res/cool-photo.jpg

两个问题:

  1. 第二个示例中传递给/wiki/cool-stuff的GET变量未传递给 index.php
  2. 访问/res(不是/res/ !!)突然在我的浏览器中显示/res/?uri=res index.php { {1}}。相反,访问uri=res/会向我显示带有/res/ index.php ,并且浏览器中的网址会保留(可以)。
  3. .htaccess

    uri=res/

    如何实现理想的行为?

1 个答案:

答案 0 :(得分:1)

  1. 尝试使用查询字符串追加标记,QSA

  2. 使尾部斜杠可选 - 在Regex中,这是通过添加?来实现的。

  3. .htaccess

    RewriteEngine on
    RewriteBase   /subthing/
    
    RewriteCond   %{REQUEST_URI}    !/res(/.*)?$
    RewriteCond   %{REQUEST_URI}    !index.php
    RewriteRule   (.*)              index.php?uri=$1 [QSA]
    

    请注意,我已在/res文件夹中调整了正则表达式,以便重定向/resabc(如果斜杠是唯一的可选部分,则以res开头的任何内容都匹配。< / p>

    Apache Mod_Rewrite Documentation

相关问题