views:

827

answers:

2

I'm trying to do something with .htaccess that I'm not sure can be done.

First thing I did is hide the .php extensions using the following code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Works great.

Now what I'm trying to do and can't seem to figure out is the following:

When a user types "mywebsite.com/products?id=12345" into the browser address bar, I want the server to serve the right product page according to the ID but display it in the address bar as "mywebsite.com/product" no matter what the product ID is.

Is this possible to do? If yes how?

Thanks

A: 

That isn't really possible in a clean way. To change the address bar you need to send an actual HTTP redirect. That means you'll be getting a new request from the browser with whatever you redirected them to, and so on that second request you won't have the product ID anymore.

There are two ways you can get around this:

  • make your server stateful
  • store the product in a cookie

Both of these are bad ideas.

Why do you want to do this anyway? Do you want to make sure your users can't bookmark your products? That seems pretty user-hostile.

Laurence Gonsalves
Enkay
A: 

I ended up using ajax and sessions to make the filtering process seamless to the user. I guess what I was trying to do wasn't possible with an htacess file. Thanks anyway!

Enkay