.htaccess mod_rewrite尾部斜杠


.htaccess mod_rewrite trailing slash

我的mod_rewrite模块出现问题。如果我做/toronto/,它会相应地指示我,但是,如果我做/toronto而没有尾部斜杠,它会返回404。我需要/toronto和/toronto/都从/city_name文件夹中读取。如何避免尾部出现斜杠,下面是我的代码:

重写引擎打开重写Base/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]

RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]

该规则明确表示URI必须以斜杠结尾,因此"toronto"不匹配任何规则。要使斜线可选,请使用?操作员:

RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]