-2

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

If for example I had the URL:

http://www.example.co.uk/products.php?id=1

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

http://www.example.co.uk/products/1

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:

http://www.example.co.uk/products.php?id=1

to

http://www.example.co.uk/products/lemon

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.

Ash
  • 792
  • 1
  • 6
  • 23
  • In your case i will do it with just name not with id. on php. – MRustamzade Sep 28 '16 at 22:28
  • I know, that is how I do it myself as stated in the question. I am just curious to if it is actually possible to do it this way :) – Ash Sep 28 '16 at 22:30
  • if you want `.php?id=1` to `/lemon` then yes you can, if you want more will more `rewriterules` – xYuri Sep 28 '16 at 22:35
  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Croises Sep 28 '16 at 23:48

2 Answers2

1

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 :)

Dusan Bajic
  • 10,249
  • 3
  • 33
  • 43
  • 1
    Thank you this answered my question perfectely. I know it most likely shouldn't be done, I was just curious to if it was actually possible. Here be dragons ^_^ – Ash Sep 29 '16 at 07:51
-1

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 domain.com/products/1 server rewrite it to products.php?id=1

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

User input domain.com/products/3 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

xYuri
  • 369
  • 2
  • 17
  • I understand this, but it doesn't answer my question. I'm asking is it possible to do this dynamically WITHOUT including product_name in the URL. I completely understand how to do basic rewrite rules, i'm curious if there is an advanced technique to literally pull the variable to the rewrite rule directly from the database or some other technique. – Ash Sep 28 '16 at 23:08
  • you mean the user don't see product_name? link like this > http://domain.com/products/lemon? – xYuri Sep 28 '16 at 23:11
  • or domain.com/products/ only? – xYuri Sep 28 '16 at 23:12
  • Literally the user input is only domain.com/products/1 but the rewrite rule returns domain.com/products/lemon, dynamically, is there anyway for the rewrite rule to pull the information from the database where id=1 or anyway to parse this info from the database externally to the rewrite rule. The answer to my question is probably no, but I'm curious to if there is a way of doing this, even if it is crazy. – Ash Sep 28 '16 at 23:14
  • then [Hakan's](http://stackoverflow.com/a/39758650/4977144) answer will do the job `RewriteRule ^products/1$ products.php?product_name=lemon` – xYuri Sep 28 '16 at 23:22
  • It's not a dynamic solution, would require writing a rule for every variable id of products. – Ash Sep 28 '16 at 23:24
  • and thats what my answer about **yes you can** – xYuri Sep 28 '16 at 23:26
  • Your answer is a standard rewrite rule that requires product_name to be included in the URL, I am completely aware that this would work, I already do it. My question is if it is possible to in anyway to retrieve the product_name variable without including the actual variable in the URL and as a dynamic solution. – Ash Sep 28 '16 at 23:29
  • you just don't understand what rewriterule does, again **My Answer Doesn't Require `product_name` In The URL** – xYuri Sep 28 '16 at 23:38
  • To clarify, I want to pull the variable to the .htaccess rewrite rule, not to the actual PHP page, I know how to do rewrite rules, I know how to query data from a database using PHP. You are not understanding my question, I understand how rewrite rules work. My question is literally: Can an Apache .htaccess rewrite access a variable NOT CONTAINED IN THE URL. All your examples include the variable in the URL which is not what I am asking. – Ash Sep 28 '16 at 23:39
  • you need to so some research about `.htaccess` if you want it to rewrite product/xxx to product.php?product_name=yyy then you will need to write a rule for each string – xYuri Sep 28 '16 at 23:42