重写条件不起作用

时间:2013-12-18 13:41:26

标签: regex apache .htaccess mod-rewrite redirect

在我的php - 项目中,我有以下文件夹结构。

/ [root]
|-- ... [some other folders]
|-- util/            // containing js,css files; should be accessible for anyone.
|-- client/          
    |--data/         // contains files which can be uploaded by users
       |-- private/  // should only be accessible for logged in users
       |-- public/   // should be accessible for anyone.
|-- ... [some other folders]
|-- index.php

我想实现以下行为:

  1. 如果有人直接访问util /他之内的任何内容,应该只是得到他所要求的内容,如果它不是目录。
  2. 如果有人想要访问client/中的任何文件,则应将其重定向到index.php。 例如,有人输入了网址www.test.com/client/data/private/test.jpg,服务器应该将请求作为index.php?request1=client/data/private/test.jpg
  3. 其他所有内容都应改写为index.php?request2=$1
  4. 我无法按照预期获得第2点功能。

    我使用以下.htaccess文件来处理:

    RewriteEngine On
    
    # allow access to all files within util/  WORKS!!
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)(util)($|/) - [L]
    
    # here i have problems.. how can i achieve, that access to folder client/ is rewritten to index.php?request1=$1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^client
    RewriteRule ^(.*)$ index.php?request1=$1 [QSA,L]
    
    # rewriting everything else   WORKS!!
    RewriteRule ^(.+)$ index.php?request2=$1 [QSA,L]
    

    我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

试试这段代码:

RewriteEngine On

# allow access to all files within util/  WORKS!!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^util($|/) - [L]

# here i have problems.. how can i achieve, that access to folder client/ is rewritten to index.php?request1=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^client(/.*|)$ index.php?request1=$1 [QSA,L]

# rewriting everything else WORKS!!
RewriteRule ^((?!index\.php).+)$ index.php?request2=$1 [QSA,L]