Kevlar
Kevlar

Reputation: 334

keeping GET variables after URL rewrite

im trying to rewrite my product page URLs which are dynamic

an example of one is:

product.php?id=780-1AC930D

and Im trying to rewrite it to:

product/780-1AC930D

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 FollowSymLinks
RewriteEngine On
RewriteBase / 
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 FollowSymLinks
RewriteEngine On
RewriteBase / 

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

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]

## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]

## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l

## don't do anything
RewriteRule ^ - [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 EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $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?

Upvotes: 0

Views: 6480

Answers (2)

anubhava
anubhava

Reputation: 785651

Most likely problem is not using L flag in your first rule. Have it like this:

RewriteRule ^product/(.+)$ /product.php?id=$1 [QSA,NC,L]

Upvotes: 1

Sumurai8
Sumurai8

Reputation: 20737

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.

Upvotes: 3

Related Questions