Udhayakumar
Udhayakumar

Reputation: 271

Change the URL structure using PHP

I want the URL structure like

 www.xyz.com/variable/value

I have one form with action URL is same page only and method GET.. When the user enter the value and submit the form i need the url like above in the url bar. I knew if usually using GET method the url like

 www.xyz.com?variable=value 

only. But I don't want this.. Help me if anyone know the answer.

<?php
$get_variable = $_GET['variable'];
?>

<html>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" name="variable" />
    <input type="submit" value="save"> 
</form>
</html>

Upvotes: 0

Views: 200

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

If you want to rewrite your urls using mod_rewrite, Try the following code in your htaccess file :

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /?$1=$2 [QSA,NC,L]

This will rewrite "example.com/variable/value" to "example.com/?variable=value.

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16772

If I've understood you correctly, you're looking for something like:

<?php
$get_variable = $_POST['variable'];
echo $_SERVER['PHP_SELF'] . "/" . $get_variable;
?>

<html>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" name="variable" />
    <input type="submit" value="save"> 
</form>
</html>

Note: action="<?php ehco $_SERVER['PHP_SELF'];?>" this is definitely not going to work. Watch out for ehco.

Upvotes: 1

Related Questions