如何让 modrewrite 将 url 转换为 php 文件名?
mysite.com/part1 (or mysite.com/part1/) --> part1.php
mysite.com/part1/part2 (or mysite.com/part1/part2/ --> part1_part2.php
如何让 modrewrite 将 url 转换为 php 文件名?
mysite.com/part1 (or mysite.com/part1/) --> part1.php
mysite.com/part1/part2 (or mysite.com/part1/part2/ --> part1_part2.php
# turn on rewrite engine
RewriteEngine On
# remove trailing slash
RewriteRule ^(.*)/$ $1 [L]
# translate slash to underscore
RewriteRule ^(.*)/(.*)$ $1_$2 [L]
# if there is no .php extension, add it
RewriteCond %{REQUEST_URI} !^.*\.php$
RewriteRule ^(.*)$ $1.php [L]
您可以考虑更多条件,例如不操作现有文件/目录并附加查询字符串(QSA
重写规则的参数)