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


How do I modify query strings in the URL to make them more user friendly

我的 .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

任何帮助将不胜感激!

最后插入以下 2 条新规则:

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:还要确保在上面的所有RewriteRule行中使用L标志。