Remove .php and id from url and replace slash

I tried a lot for URL rewriting rules in htaccess, but now I'm stuck. i have to change this url

products.php?id=31

to

products/31 

I used

   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]

using this, I get the following result:

products?id=31

But that does not work. Any ideas?

+4
source share
3 answers

Enter the complete .htaccessfollowing:

  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]
+2
source

you need to use it like that

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

use the htaccess rule below

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

All Articles