htaccess查询字符串转换

时间:2016-06-13 09:59:36

标签: php .htaccess mod-rewrite

我有以下网址结构:

http://example.com/file.php?section=sectionName

我想将其转换为:

http://example.com/file/sectionName

目前的尝试似乎不起作用:

RewriteRule ^(.*)$ file.php?section=$1 [L,QSA]

并且似乎干扰了从文件名中剥离.php结尾的另一次重写:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]

1 个答案:

答案 0 :(得分:1)

您可以使用此htaccess:

RewriteEngine On

#rewrite /file => file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
#rewrite /file/foobar => file.php?section=foobar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^file/([^/]+)/?$ /file.php?section=$1 [NC,L]