Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert query string to url slug on form submit get method in php?

I'm developing a website in php and this is my first site using php and I'm new to php.

The site contains 2 pages, index.php and info.php

The index.php has the below form,

<form action="info.php" method="get">
    <input type="text" name="username" />
    <input type="text" name="company" />
    <input type="email" name="email" />
    <button type="submit">Click to Proceed!</button>
</form>

When the user enter and submit the details. It redirects to the next page and the url contains the query string like,

http://localhost/info?username=john&company=zend&[email protected]

I want to display the above url like this,

http://localhost/info/john/zend/[email protected]

and to get the values from url using $_GET['username'],$_GET['company'] and $_GET['email']

I tried the lot of rewrite rule including the below in htaccess,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^([\d\w-]+)$ info?username=$1&company=$2&email=$3 [L,QSA]
RewriteRule ^([\d\w-]+)$ info?username=$1&company=$2&email=$3 [QSA]
RewriteRule ^(.*)$ info?username=$1&company=$2&email=$3 [L,QSA]
RewriteRule ^([a-zA-Z0-9-/]+)/([0-9]+)$ info?username=$1&company=$2&email=$3 [L,QSA]
RewriteRule ^([a-zA-Z0-9-/]+)/([0-9]+)$ info?username=$1&company=$2&email=$3 [QSA]

but nothing works.

I tried this and Clean URLs for search query? too.

would somebody help me with this issue.

like image 541
Gopinath Perumal Avatar asked Sep 17 '14 12:09

Gopinath Perumal


Video Answer


1 Answers

The flow is this.

submit your form to route.php

here is the code to route.php

if(isset($_GET['username']) && isset($_GET['company'])  && isset($_GET['email']) )
    $url = '/info/'.$_GET['username'].'/'.$_GET['company'].'/'.$_GET['email']
header('Location: '.$url);

In your .htaccess

RewriteRule  ^info/(.+)/(.+)/(.+)$ info.php?username=$1&company=$2&email=$3 [L,QSA]
like image 181
MontrealDevOne Avatar answered Sep 30 '22 18:09

MontrealDevOne