0

I get some basic results from a mysql database and list it on a page, on the page I've added the links with the variable user_id:

href=\"http://localhost/TESTING/template.php?user_id=$user_id\" >click here

On the template.php file I'll get the $user_id and display the relevant full data.

The thing is I don't want user_id=249 http://localhost/TESTING/template.php?**user_id=**249 to show in the url I'd like something like "http://localhost/TESTING/template.php/249".

I've been looking into this for a few days now with no success, is this possible? Or does the template file need the user_id=$user_id to be displayed in order to get the variable?

=====================

To update: I added the code

RewriteEngine On

RewriteBase /TESTING/

RewriteRule ^template.php/([0-9]+)(/)?$ template.php?user_id=$1 [L]

Changed the link variable to href="localhost/TESTING/template.php/{$user_id}" >click here

The re-write now works, but I'm not getting the variable with $user_id = $_GET["user_id"]; on the template.php page

I added the following to the template.php page to see whats being passed through the url:

<?php
ini_set('display_errors',1);
echo '<b>$_GET Variables</b><pre>';
var_dump( $_GET );
echo '</pre>';
?> 

and got the following response:

$_GET Variables

array(1) { ["user_id"]=> string(2) "21" }

How to get the variable?

===========

To update now works! {$user_id} in the link not {user_id} - could of swore i tried both ways.

1
  • also let us know what server/set up i.e windows/unix Commented Feb 18, 2011 at 14:19

2 Answers 2

2

Note that mod_rewrite only works on an Apache server.

RewriteEngine On

RewriteBase /TESTING/

RewriteRule ^template.php/([0-9]+)(/)?$ template.php?user_id=$1 [L]
Sign up to request clarification or add additional context in comments.

11 Comments

This looks like something I've been trying to get to work but this still shows the url as: localhost/TESTING/template.php?user_id=249
I'm running xampp btw and have checked if mod_rewrite is enabled.
I don't think you understand how mod_rewrite works. It doesn't rewrite the URLs you output, it only rewrites the URL requests it receives to another URL.
When you link to the page, don't use href="localhost/TESTING/template.php?user_id=$user_id" >click here.... Use: href="localhost/TESTING/template.php/{$user_id}" >click here
thanks radu, getting somewhere now, it displays the page with the new url but without any of the variables, so my mysql_query on the template page isn't finding the {user_id} variable. this is what i use: $user_id = $_GET["user_id"];
|
1

I don't want to be inpolite, but there are a lot of manuals in the interwebs, e.g.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

which explains, with examples, everything you need. What have you tried so far? So we can explain what's wrong.

1 Comment

Well, for example the post by radu is similar to what I've been trying but with no luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.