Gopinath Perumal
Gopinath Perumal

Reputation: 2318

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.

Upvotes: 7

Views: 2657

Answers (2)

MontrealDevOne
MontrealDevOne

Reputation: 1044

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]

Upvotes: 1

Vicky
Vicky

Reputation: 613

Check with a simple rule,if rewrite is working. Make sure rewrite module is enabled.

 RewriteRule  ^info/(.+)/(.+)/(.+)(/*)$ info?username=$1&company=$2&email=$3 [L,QSA]

Upvotes: 0

Related Questions