1

I have successfully created clean url for my project.. now what i need to do is add variables to URL..

localhost/user1/file?action=add
localhost/user2/file2?action=delete

where user and file are already rewritten i dont want it to be like localhost/user/file/add because localhost/user/folder/file will be mistaken to to the action parameter.. please help

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Jeyanth Kumar
  • 1,589
  • 3
  • 23
  • 48

3 Answers3

1

Try using the ampersand instead of question mark:

localhost/user2/file2&action=delete

In your htaccess, the rewrite rule might look something like this:

RewriteRule ^user([0-9]+)/file([0-9]+)$ /page\.php?user=$1&file=$2

As you can see, the question mark is already there even though it is masked in the address bar. Appending another variable to the query string would require the ampersand for successful concatenation.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • problem with some webservices like openID just go back to the url from where it's posted and they add a questionmark. But I understand in this case it works. – Vince V. Aug 16 '11 at 14:54
0

You need to get the url and start parsing the url from the question mark. I would save the contents then to an array, so that you've got a key and a value.

$uri = $_SERVER["REQUEST_URI"];

$questionMark = explode('?',$uri);

$questionMark[1] is the action=delete then. There are probably better ways then using explode() method here, but I just wanted to show how you get the string.

Vince V.
  • 3,115
  • 3
  • 30
  • 45
-1

You can read GET variables in PHP by accessing the global $_GET array:

http://php.net/manual/en/reserved.variables.get.php

In your example, the php file that is used for handling files would be able to read in:

echo $_GET['action']; // 'add' or 'delete'
Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
  • 1
    the GET would return nothing. because he already uses a get in the background (mod_rewrite) – Vince V. Aug 16 '11 at 14:44
  • mod_rewrite rewrites the url for the user1/file part. The question was how to deal with the rest. As you can see from AlienWebguy's answer, the page.php?user=$1&file=$2 means that page.php will receive these variables via GET. – Aram Kocharyan Aug 16 '11 at 14:50