2012-07-24 60 views
0

我试图设置一个.htaccess文件,该文件将每个请求URL作为GET传递到名为index.php的文件中。唯一的例外是请求URL指向目录res现有文件夹的mod_rewrite条件

实例:

/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在我的浏览器中 a nd index.phpuri=res/。改为访问/res/,显示我index.phpuri=res/并且浏览器中的URL保持不变(这是可以的)。

的.htaccess

RewriteEngine on 
RewriteBase /subthing/ 

RewriteCond %{REQUEST_URI} !/res/(.+)$ 
RewriteCond %{REQUEST_URI} !index.php 
RewriteRule (.*)    index.php?uri=$1 

我怎样才能达到预期的行为?

回答

1
  1. 尝试使用查询字符串,附加标志,QSA

  2. 让尾随斜线可选 - 在正则表达式,这是通过添加?实现。

.htaccess

RewriteEngine on 
RewriteBase /subthing/ 

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

请注意,我已经调整了正则表达式的/res文件夹会导致/resabc被重定向(如果斜杠是唯一可选件,与res开始任何将匹配。

Apache Mod_Rewrite Documentation

+0

感谢您与'[QSA]'的TIPP。这一工程鳍即这似乎是因为Firefox的'/ res'在URL栏中变成了'/ res /?uri = res'。它不会出现在Firefox中。我想'/ res /'(没有任何尾随的东西)也被重定向到'index.php',你能告诉我如何实现这个吗? – 2012-07-25 12:40:28

+0

@NiklasR这将重定向/ res /如果我没有记错。 Firefox缓存已知的重定向,可能会变得混乱。清除浏览记录,然后请让我知道是否仍有问题。 – 2012-07-25 13:44:35

相关问题