2

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

任何帮助是极大的赞赏!

4

1 回答 1

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

于 2013-11-10T07:08:05.687 回答