mod_rewrite can only rewrite / redirect the requested URIs, not the ones contained in your HTML documents. Therefore, first make sure that your PHP application prints the correct URIs, therefore /stories/17.html instead of /stories.php?id=17 .
After that, you can use the rule suggested by Jose Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Although redirecting /stories.php?id=17 requests from outside to /stories/17.html and then internally back to /stories.php?id=17 possible, itβs not /stories.php?id=17 practice, as this will double the number of requests. But this is the rule for this:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s] RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$ RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]
Gumbo source share