简体   繁体   中英

.htaccess mod_rewrite trailing slash

I'm having issues with my mod_rewrite module here. If I do /toronto/ it will direct me accordingly, however, if I do /toronto without the trailing slash it comes back with a 404. I need /toronto and /toronto/ to both read from the /city_name folder. How can I avoid having the trailing slash, here's my code:

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]

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

This rule explicitly says that the URI has to end with a slash, so 'toronto' matches no rules. To make the slash optional, use the ? operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM