0

如何让 modrewrite 将 url 转换为 php 文件名?

mysite.com/part1       (or mysite.com/part1/)       --> part1.php
mysite.com/part1/part2 (or mysite.com/part1/part2/  --> part1_part2.php
4

1 回答 1

1
# 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重写规则的参数)

于 2012-11-18T11:21:54.410 回答