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

问题描述

原始网址 - 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=$1&s=$2 [L]

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

请帮忙

解决方法

根据您展示的样品,您可以尝试以下操作吗?在测试您的网址之前,请务必清除浏览器缓存。

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 ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?id=$2&s=$3 [L]