如何修改URL中的查询字符串以使其更加用户友好

时间:2013-11-09 17:13:12

标签: php regex apache .htaccess mod-rewrite

我的.htaccess文件中有以下内容,该文件删除.php扩展名,如果不是目录,则删除余数之后的斜杠。但是,我想修改它以更改查询字符串。

AddType text/x-component .htc
RewriteBase /

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

我想将{root} / file?id = 1更改为/ file / 1 和 我想将{root} / directory / file?id = 1更改为{root} / directory / file / 1

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

最后插入这两个新规则

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)/?$ /$1?id=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$ /$1/$2?id=$2 [L,QSA]

PS:还要确保在上面的所有L行中使用RewriteRule标记。