2013-12-18 67 views
2

在我的php -project中,我有以下文件夾結構。重寫條件不起作用

/ [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。 例如有人輸入url www.test.com/client/data/private/test.jpg服務器應該將請求作爲index.php?request1=client/data/private/test.jpg
  3. 其他的一切應該重寫index.php?request2=$1

我沒能獲得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] 

我到底錯在這裏做什麼?

+0

應與'/客戶/ DIR1/dir2'發生什麼地方不要求特定_FILE_?是否應該重寫爲index.php,還是應該修改目錄訪問權限? –

+0

這應該被RewriteRule 3抓住,或者拋出這些http錯誤401,404中的一個。但是我寧願規則3 – emfi

+0

啊等一下 - 'REQUEST_URI'應該在匹配成爲'^ /客戶端「,因爲這是標題的結構。這與'RewriteRule'預期在目錄上下文中匹配的方式形成對比。試試看,這可能就是你需要的一切。其他一切看起來都適合我。 –

回答

1

試試這個代碼:

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] 
+0

RewriteRule:無法編譯正則表達式'^((?! index \\。php。+)$' – emfi

+0

我現在已經嘗試編輯過代碼了。 – anubhava

+0

完美!你已經救了我的一天,謝謝。 – emfi