3

我的 mod_rewrite 运行良好,但在 /calgary/ 之后它无法识别任何内容

那么,如果我去,/calgary/login.php它只看到index.php页面?它不会识别/login.php页面?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /city_name

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
    RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
    RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>
4

2 回答 2

1

您需要交换这两条规则:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /city_name

    # This used to be the 2nd rule
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
    RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
    # there used to be a "?" here, remove it ----v
    RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]

    # this used to be the first rule
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
</IfModule>

限制较少的规则(匹配的规则(.*)/?$)将/calgary/login.php在第二组规则开始执行其操作之前完全匹配,这似乎被重写为/city_name/?login.php?page=calgary.

这真的是你想要的吗?目标中有两个?。也许你只想要/city_name/$2.php?page=$1

于 2012-08-29T19:12:02.480 回答
0

尝试从第一个重写规则中删除“L”,这会告诉 Apache 如果找到匹配项则停止处理进一步的规则。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /city_name

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/?$ /city_name/index.php?page=$1 [QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
    RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
    RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>
于 2012-08-29T19:15:47.677 回答