2013-03-01 131 views
3

我有一個.htaccess中爲我的網站如何爲.htaccess編寫多個條件和規則?

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{SCRIPT_FILENAME} !-f 
    RewriteCond %{SCRIPT_FILENAME} !-d 
    RewriteRule ^(.*)$ profile.php?membername=$1 [L] 
    RewriteRule ^poem/(.*)/?$ poem.php?id=$1 [L,NC] 
</IfModule> 


# Works 
RewriteRule ^(.*)$ profile.php?membername=$1 [L] 
# Does not work 
RewriteRule ^poem/(.*)/?$ poem.php?id=$1 [L,NC] 

如果這一切改變這種波紋管,則結果相反。

# No longer works: 
RewriteRule ^(.*)$ profile.php?membername=$1 [L] 
# Now works: 
RewriteRule ^poem/(.*)/?$ poem.php?id=$1 [L,NC] 


<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{SCRIPT_FILENAME} -f 
    RewriteCond %{SCRIPT_FILENAME} -d 
    RewriteRule ^(.*)$ profile.php?membername=$1 [L] 
    RewriteRule ^poem/(.*)/?$ poem.php?id=$1 [L,NC] 
</IfModule> 

發生了什麼事?我不知道。

回答

2

看起來你需要做的是在REQUEST_FILENAME上保留負面測試並顛倒規則的順序。否則,更通用的^(.*)總是首先匹配,並且否決^poem/

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    # Negative conditions here... 
    # Using REQUEST_FILENAME instead of SCRIPT_FILENAME 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    # Match the more specific rule first... 
    RewriteRule ^poem/(.*)/?$ poem.php?id=$1 [L,NC] 

    # Match the generic rule last. 
    RewriteRule ^(.*)$ profile.php?membername=$1 [L] 
</IfModule> 
+0

謝謝!我剛剛發現,如果我將RewriteRule^poem /(.*)/?$ poem.php?id = $ 1 [L,NC]放在條件完美的條件之前。 – Joshua 2013-03-01 01:20:51