从url中删除.php和id,并用斜杠替换

我已经在htaccess尝试了很多url重写规则,但我现在卡住了.我必须改变这个网址

products.php?id=31

products/31 

我用过

   Options +FollowSymLinks -MultiViews
   # Turn mod_rewrite on
   RewriteEngine On
   RewriteBase /

  ## don't touch /forum URIs
  RewriteRule ^forums/ - [L,NC]

  ## hide .php extension snippet

  # To externally redirect /dir/foo.php to /dir/foo
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
  RewriteRule ^ %1 [R,L]

  # To internally forward /dir/foo to /dir/foo.php
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^(.*?)/?$$1.php [L]

使用这个我得到以下结果:

products?id=31

但这不起作用.有任何想法吗?

最佳答案 你有完整的.htaccess像这样:

  Options +FollowSymLinks -MultiViews
  # Turn mod_rewrite on
  RewriteEngine On
  RewriteBase /

  ## don't touch /forum URIs
  RewriteRule ^forums/ - [L,NC]

  RewriteCond %{THE_REQUEST} \s/+products(?:\.php)?\?id=([0-9]+) [NC]
  RewriteRule ^ products/%1? [R,L]

  RewriteRule ^products/([0-9]+)/?$products.php?id=$1 [L,QSA]

  ## hide .php extension snippet
  # To externally redirect /dir/foo.php to /dir/foo
  RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
  RewriteRule ^ %1 [R,L]

  # To internally forward /dir/foo to /dir/foo.php
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^(.+?)/?$$1.php [L]
点赞