php - 如何将查询字符串转换为 url slug on form submit get method in php?

标签 php forms .htaccess wamp

我正在用 php 开发一个网站,这是我第一个使用 php 的网站,我是 php 的新手。

该站点包含 2 个页面,index.php 和 info.php

index.php 具有以下形式,

<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>

当用户输入并提交详细信息时。它重定向到下一页并且 url 包含查询字符串,例如,

http://localhost/info?username=john&company=zend&email=beast@example.com

我想像这样显示上面的url,

http://localhost/info/john/zend/beast@example.com

并使用 $_GET['username']、$_GET['company'] 和 $_GET['email'] 从 url 获取值

我在 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]

但没有任何作用。

我试过了 thisClean URLs for search query?也是。

谁能帮我解决这个问题。

最佳答案

流程是这样的。

将表单提交到 route.php

这是route.php的代码

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

在你的 .htaccess 中

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

关于php - 如何将查询字符串转换为 url slug on form submit get method in php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25890131/

相关文章:

php - 使用我的公钥加密 php 联系表

php - 没有 .htaccess 的 Mod 重写

选择下拉菜单中的 CSS 样式第一个选项

reactjs - 什么是 onChange={(e) => setName(e.target.value)}?在 React 中

php - Zend框架2 : redirect toRoute shortcircuit not triggering

php - 使侧边栏中的所有图像大小相同?

.htaccess - 修复 IP 规范化错误

apache - .htaccess - 删除扩展

php - 正则表达式 - 排除字符串的否定前瞻

php - 在 php 中解析 json 时出错