3

我的网站的 .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>

这是怎么回事?我不知道。

4

1 回答 1

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>
于 2013-03-01T01:08:01.133 回答