2016-09-22 61 views
1
<Directory /var/www/website/html> 
     Options +SymLinksIfOwnerMatch -Indexes 
     AllowOverride All 
     RewriteEngine on 

     # Enforce removal of trailing slash 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)/$ /$1 [L,R=301] 

     # If the user is performing a search 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteCond %{QUERY_STRING} ^query=([^&]+) 
     RewriteRule ^(.*)$ index.php?uri=$1&query=%1 [L,B] 

     # If the user has a token set 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteCond %{QUERY_STRING} ^token=([^&]+) 
     RewriteRule ^(.*)$ index.php?uri=$1&token=%1 [L,B] 

     # If not existing file/directory, redirect to index.php 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ index.php?uri=$1 [L,B] 
</Directory> 

當我張貼上面的QUERY_STRING條件服務器上的數據到一個目錄,看來該RewriteRule被清除後的數據。重寫規則結算後的數據

我期待修改此RewriteRule,以便在滿足這些條件時仍可訪問發佈數據。

+0

這個.htaccess中有更多的規則嗎? – anubhava

+0

@anubhava在'apache2.conf'中更新了完整的目錄列表,我所指的規則是用「如果用戶有令牌集」註釋的規則 – jskidd3

+0

您的發佈請求URL是否有尾部斜線? – anubhava

回答

1

有像這樣以避免重定向時REQUEST_METHODPOST

<Directory /var/www/website/html> 
     Options +SymLinksIfOwnerMatch -Indexes 
     AllowOverride All 
     RewriteEngine on 

     # Enforce removal of trailing slash except for POST request 
     RewriteCond %{REQUEST_METHOD} !POST 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)/$ /$1 [L,R=301,NE] 

     # skip all files and directories from rewrite rules below 
     RewriteCond %{REQUEST_FILENAME} -d [OR] 
     RewriteCond %{REQUEST_FILENAME} -f 
     RewriteRule^- [L] 

     # If the user is performing a search 
     RewriteCond %{QUERY_STRING} ^query= 
     RewriteRule ^(.*)$ index.php?uri=$1&query=%1 [L,B,QSA] 

     # If the user has a token set 
     RewriteCond %{QUERY_STRING} ^token= 
     RewriteRule ^(.*)$ index.php?uri=$1&token=%1 [L,B,QSA] 

     # If not existing file/directory, redirect to index.php 
     RewriteRule ^(.*)$ index.php?uri=$1 [L,B,QSA] 
</Directory> 

我重構一些規則,以避免多餘的條件。還可以使用QSA標誌追加上一個查詢字符串。