htaccess - 删除 .php 扩展并保留变量

htaccess - remove .php extension and keep variables

原始 url - example.com/stars.php?id=9&s=lorem-ipsum
想成为 - example.com/stars/9/lorem-ipsum

这是我的尝试 - 出现错误 500

RewriteCond %{THE_REQUEST} \s/stars\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.php$ /stars.php?id=&s= [L]

.htaccess 和 stars.php 放在根目录中 - example.com

请帮忙

根据您展示的示例,请您尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##Rule to handle external redirection to user friendly URL here.
RewriteCond %{THE_REQUEST} \s/(stars)\.php\?id=(\d+)&s=([^\s]*) [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]

##Rule to handle non-existing files/directories should be served by php files in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ .php?id=&s= [L]