mod_Rewrite将路径转换为复杂的文件名

时间:2012-11-17 20:43:27

标签: .htaccess mod-rewrite

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

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

1 个答案:

答案 0 :(得分: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参数来重写规则)

相关问题