我正在用 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]
但没有任何作用。
我试过了 this和 Clean 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/