Php – keeping GET variables after URL rewrite

.htaccess, php, regex, url

im trying to rewrite my product page URLs which are dynamic

an example of one is:


and Im trying to rewrite it to:


i need the variable to still pass through with the URL though but currently its not working for me.

I have a redirect at the top of the product.php page something like this:

$prodID = $_GET['id'];    if(!isset($prodID) && $prodId == ""){        header( 'Location: http://www.***********.com' ) ;    }           else{

and I know the GET variable isnt passing through because it keeps redirecting me.

the code I have for the HTACCESS file is this:

 Options FollowSymLinksRewriteEngine OnRewriteBase / RewriteRule ^product/(.*)$ /product.php?id=$1 [QSA]

and the HTML for the URL is:

<a href = "product/<?php echo $product[$i]['catID'];?>">

does anybody have a clue why this isnt working?

EDIT to give more information.

This is my entire HTACCESS file

Options FollowSymLinksRewriteEngine OnRewriteBase / RewriteRule ^product/(.*)$ /product.php?id=$1 [QSA]## If the request is for a valid directoryRewriteCond %{REQUEST_FILENAME} -d [OR]## If the request is for a valid fileRewriteCond %{REQUEST_FILENAME} -f [OR]## If the request is for a valid linkRewriteCond %{REQUEST_FILENAME} -l## don't do anythingRewriteRule ^ - [L]RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)&subCat=([^\s&]+)\s [NC]RewriteRule ^ /%1/%2/%3? [R=301,L]RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /$1.php?cat=$2&subCat=$3 [L,QSA]RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)\s [NC]RewriteRule ^ /%1/%2? [R=301,L]# REMOVE PHP EXTENSIONSRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ $1.php [L,QSA]

I just moved RewriteRule ^product/(.*)$ /product.php?id=$1 [QSA] from above the "# REMOVE PHP EXTENSIONS" to below the RewriteBase and it appears to fix my problem.

The other rewrite rules are for my category and sub category pages before you actually make it to a product page.

So im curious as to why placing this product.php line above this other stuff has done it?

Best Solution

The problem was that an other rule matched the request, before the rule you wanted to match. In this case the rule that matched your request was:

RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]

The L-flag will stop rewriting for this cycle. It therefore never reached

RewriteRule ^product/(.*)$ /product.php?id=$1 [QSA]

See this documentation for information about flags and this documentation for more information about mod_rewrite in general.