5

I want to know how I can pass parameters between pages through the URL without having to add variables eg:

mydomain.com/file.php?var1=val1&var2=val2&...varN=valN

I want to use it as follows:

mydomain.com/file.php?val1-val2-...-valN

I also see in some website the URL is in the following format

mydomain.com/file/####

which redirects to another page without changing the URL as if this is the URL to the file.

2 Answers 2

13

You should use .htaccess

Here's an example:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [NC,L]

This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.

Example:

mysite.com/20/this_is_a_new_article/

Would actually mean

mysite.com?var1=20&var2=this_is_a_new_article

Of course, in your index.php file you can simply take the values using

$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

Cheers!

Sign up to request clarification or add additional context in comments.

3 Comments

I have read somewhere that this is done by PHP and I saw an example but I can`t find it after I changed my PC :( ... thats why I want to do it using the PHP ... what I remember about it that it used the explode function ...
You could do it with PHP, but I wouldn't recommend it. Anyway, you can simply use $_SERVER['REQUEST_URI'] and explode the result. Example: explode('/', $_SERVER['REQUEST_URI']); This would return an array that you can use. Still, you need htaccess to redirect every URL to the page on which you put this code. You can also use ie.php.net/manual/en/function.parse-url.php
Hey bt if i pass only one parameter in URL instead of 2 it shows 404 error. What could be the solution for that? coz sometime i want to pass 1 variable sometimes 2 or more what then?
0

Your third example is an example of how REST identifies a server side resource. What you are talking about here sounds very much like REST will do what you want. I would suggest starting here (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services).

Comments

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.