Apache重写.php以及之后的所有内容

时间:2012-09-11 05:45:45

标签: apache rewrite

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

上述工作至于从我的所有网址中删除.php。是否有可能删除网址后的所有参数(如果存在)?例如,将site.com/index.php?id=24重写为site.com/index

1 个答案:

答案 0 :(得分:1)

在%1之后添加?

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1? [R,L,NC]
# here ---------^

当重写规则的目标URI中有?时,除非有QSA标记,否则规则不会附加查询字符串。