Can an Apache .htaccess rewrite access a PHP variable not contained in the URL

By : Ash

I assume this is not possible, but would like to double check.

If for example I had the URL:

I know using basic rewrite rules I can change the URL to anything using the id variable, for example:

Lets say for example the database also has the row "product_name". It is not declared in the URL. Is it possible to access the value for the product_name row where "id = 1" in a rewrite rule without adding the "product_name" variable to the URL?

For example if the database entry values were:

 id = 1
 product_name = 'lemon'

Could you create a rewrite rule that would change the following dynamically:


I know the ideal situation would be to just parse "product_name" instead of "id" (I already do this). I am just curious to if this is actually possible.

Edit: I think people are confused by my question, I am asking if there is a way to do this WITHOUT putting the product_name variable into the source URL.

Edit2: Why do people keep replying with how to do a normal rewrite rule. That isn't my question, I am not asking how you do rewrite rules or asking how they work, I know this already as explained in my original question if you actually bother to read it.

I am asking if it is possible to access a variable from the database that is not included in the URL. Literally is there anyway at all, even if it is crazy and bad practice to do so. Can you access the row in the database product_name, using just the id row in the URL from the .htaccess file (Not from a PHP file), no is a legitimate answer if that isn't possible. Telling me how to write a rewrite rule with the variable in the URL is not answering my question so stop down voting me just because I wont accept your answer.

By : Ash


While this even might be doable (with prg: External Rewriting Program), really, just don't do it. Write a simple php (or whatever) script.

You have the prg: example here, but read the comments first :)

if you want really dynamic solution then read this:

we want to pass the product name though the url without letting the user know that we doing this:

RewriteRule ^products/(.*)$ products.php?product_name=$1 [NC]
## i.e products/lemon server will understand it like: products.php?product_name=lemon
## i.e products/something_i_dont_know rewrite it to products.php?product_name=something_i_dont_know

while the user will still see products/something_i_dont_know in his browser url bar, just like a "MASK"

if you want to pass 2 variables (not only 1) then we do it like this

RewriteRule ^products/(.*)/id/(.*)$ products.php?product_name=$1&id=$2 [NC]

if you want to pull the product info from db with id dynamically then this will do:

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

//you pull it from db like this
$id = $_GET['id'];
$sql= 'SELECT * FROM products WHERE id="'.$id.'"';

Summary User input server rewrite it to products.php?id=1

User input server rewrite it to products.php?id=2

User input server rewrite it to products.php?id=3

and so on,

note: the ( ) that comes first takes $1, the later takes $2

hope that will be easy to understand

By : xYuri

This video can help you solving your question :)
By: admin