Ok so this might not be the complete answer but should help you find your way.
You can use regex to match your desired path pattern. So for example your htaccess might look something like...
# Check if module is installed
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Check query for matching pattern and pass id, but also append additional query strings
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/)?product\/([0-9]+)$ /$1product.php?id=$2 [L,QSA]
# If not file or directory on server, send to 404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
</IfModule>
And what this does is...
1. Match the uri with a regex pattern
Regex: ^([^\/]+\/)?product\/([0-9]+)$
^
- Start of string.
([^\/]+\/)?
- matches any directory (if exists) and stores it for reuse.
product\/([0-9]+)
- Your desired path e.g. product/50
and stores the number "id" for reuse.
$
- End of string.
2. Pass captured directory and id to our file
Like so: /$1product.php?id=$2 [L,QSA]
$1
is our directory name including the trailing slash e.g. subsubsite/
$2
is our product id e.g. 50
[L,QSA]
The QSA flag means we can access additional query string parameters e.g. /product/50?show=all&updated=1
. More about flags can be found here http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
3. 404 anything not matching
Like so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
!-f
If request is not a file
!-d
If request is not a directory
/404.php
The file used for presenting a 404 error.
Getting the id...
With the above, you can get the ID within your product.php
file like so:
$id = (int)$_GET[ 'id' ];