htaccess - remove .php extension and keep variables

Viewed 68

original url - example.com/stars.php?id=9&s=lorem-ipsum
want to be - example.com/stars/9/lorem-ipsum

here is my try - getting error 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 and stars.php are placed in the root - example.com

pls help

1 Answers

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

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]
Related