mod_rewrite无法识别文件

时间:2012-08-29 18:21:14

标签: php sql mod-rewrite

我的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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试删除&#39; L&#39;从第一个重写规则,这告诉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>